My musings with Golang, Part 1

originally posted on medium

image

Renée French / Creative Commons Attribution 3.0

My musings with Golang, Part 1 Link to heading

What makes life easier Link to heading

As a lot of people in the industry I’ve been hearing about Golang since Google launched it a few years ago but it wasn’t until I saw this video about concurrency and parallelism that I actually wanted to try it out.

Although it looks very low level from a first look (no classes and explicit pointers do remind me of C) it soon shows its benefits: Large number of libraries available, helpful user base, great performance, easy testing and for such a low level looking language it actually brings a lot of high level features.

Some of my favourite things Link to heading

UTF8 Out of the Box Link to heading

As seen in the tutorial you’ll have UTF8 working on any environment. package main``import “fmt”``func main() { fmt.Println(“Hello, 世界”) }

Will return: Hello, 世界

The package fmt represents common IO use, in this case for a print line.

The binary will always look for the main package and main function to run the application.

As the testing framework will look for all packages in the current directory and break if there is more than one it forces to separate packages per folder.

Ease to import new packages Link to heading

Golang will look for the variable $GOPATH to find new packages, adding new packages is easy. cd $GOPATH/src``mkdir goncalopereira``cd goncalopereira``git clone [https://github.com/goncalopereira/goRequests.git](https://github.com/goncalopereira/goRequests.git)``go build goRequests

You can now import with the relative path to src, adding the github account name is just the Golang way to separate namespaces. import “goncalopereira/goRequests”

Testing Link to heading

Just create a new file in the same folder, include it to the same package (yes! testing is part of the package), import testing and call the function by appending the word Test to the test’s name.

In this example the test is called FindFile and the function being tested is Read. package config``import “testing”``func TestFindFile(t *testing.T) {``csvFile := “random”`` values, err := Read(csvFile)`` if err == nil {`` t.Error(“Should error trying to read non existing file”)`` }`` if values != nil {`` t.Error(“Should not have values”)`` }``}

You can now call go test

HTTP Request, Response, cleanup and even a Web Server Link to heading

Simply importing the net/http will give access to HTTP request and response objects with ease to interact with body and headers. res, err := http.Get(“http://google.com”) `` defer res.Body.Close()

And concepts like using can be replaced with defer, which will be called at the end of the response’s lifecycle and we don’t need to create a scope for the manual garbage collection. path, port := “/”, “:8888" `` http.Handle(path, h)`` err = http.ListenAndServe(port,nil)

And that’s a web server.

Structs and extensions Link to heading

Although there are no classes, to prevent global variables and scope the use of functions you can extend struct. Building the h object of the previous example. type handler struct {`` urlFormat string``}``func (v *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {``}`` h := &handler{urlFormat:“test”}

The HTTP handler interface forces to implement the ServeHTTP function.

Channels, the Go method and more Link to heading

There is a lot more to Golang, something I’m not going to write about as queues, locks and parallelism which I haven’t taken advantage of for the moment but is very well explained in the video mentioned in the introduction as well on the Effective Go page.