Essential Tools for Go Programming

0
1082
5 min read

Golang as a programming language is a pleasure to work with, but the reason for this also comes largely in part from the great community around the language and its modern tool set, both from standard distribution and third-party tools.

The go command

On a system with go installed, type go with no arguments to see its quick help menu. Here, you will see the basic go commands, such as build, run, get, install, fmt, and so on. Go ahead and take a minute to run go help on some verbs that look interesting; I promise I’ll be here when you get back.

Basic Side options

The go build and go run commands do what you think they do, as is also the case with go test, which runs any test files in the directory it is passed.

The go clean command wipe out all the compiled and executable files from the directory in which it is run. Run this command when you want to force a build to be made entirely from source again.


The go version command prints out the version and build info, as you might expect.

The go env command is very useful when you want to see exactly how your environment is set up. Running it will show where all your environment variables point and will also make you aware of which ones are still not properly set.

go doc: Which arguments did this take again?

Whenever in doubt, just give go doc a call. Just running go doc [Package Name] will give you a high-level readout of the types, interfaces, and behavior defined in this package; that is, go doc net/http will give you all the function stubs and types defined. If you just need to check the order or types of arguments that a function takes, run go doc on the package and use a tool like grep to grab the relevant line, such as go doc net/http | grep -i servecontent

This will produce just what we need!

func ServeContent(w ResponseWriter, req *Request, name string, modtime time.Time, content io.ReadSeeker)

If you need more detail on the function or type, just run the go doc command with the package and function name, and you will get a quick description of this function or type.

gofmt

This little tool is quite a time-saver. I mainly use it to ensure that my source files are stylistically correct, and I also use the -s flag to let gofmt simplify my code.

Just run gofmt -w on a file or an entire directory to fix up the files in place. After running this command, you should see the proper use of white space and indentation corrected to eight space tabs. Here is a diff of a file with poor formatting that I ran through gofmt:

Original

package main
import "fmt"
func main() {
 hello_to := []string{"Dust", "Trees", "Plants", "Carnivorous plants"}

for _, value := range hello_to {
fmt.Printf("Hello %v!n",value)
}
}

After running gofmt -w Hello.go

package main

import "fmt"

func main() {
    hello_to := []string{"Dust", "Trees", "Plants", "Carnivorous plants"}

    for _, value := range hello_to {
        fmt.Printf("Hello %v!n", value)
    }
}

As you can see, the indentation looks much better and reads way easier!

The magic of gofmt -s

The -s flag to gofmt helps clean up unnecessary code; so, the intentionally ignored values in the following code:

   hello_to := []int{1, 2, 3, 4, 5, 6}

   for count, _ := range hello_to {
       fmt.Printf("%v: Hello!n", count)
   }

Would get converted to the following after running –s:

hello_to := []int{1, 2, 3, 4, 5, 6}

    for count, _ := range hello_to {
        fmt.Printf("%v: Hello!n", count)
    }

The awesomeness of go get

One of the really cool features of the go command is that go get it works seamlessly with code hosted on GitHub as well as repositories hosted elsewhere.

A note of warning

Make sure that $GOPATH is properly set (this is usually exported as a variable in your shell). You may have a line such as “export GOPATH=$HOME” in your shell’s profile file.

Nabbing a library off of GitHub

Say, we see this really neat library we want to use called fastHttp. Using only the go tool, we can fetch the library and get it ready for use all with just:

 go get github.com/valyala/fasthttp 

Now, all we have to do is import it with the exact same path, and we can start using the library right away! Just type this and it should do the trick:

import "github.com/valyala/fasthttp"

In the event that you want to have a look around in the library you just downloaded with go get, just type cd into $GOPATH/src/[Path that was provided to get command]—in this case, $GOPATH/src/github.com/valyala/fasthttp—and feel free to inspect the source files.

I am also happy to inform you that you can also use go doc with the libraries you download in the exact same way as you use go doc when interacting with the standard library! Try it: type go doc fasthttp (you might want to tack on less since its a little bit long to type go doc fasthttp | less).

Those are only stock features and options!

The go tool is great and gets the job done, but there are also other great alternatives to some of the go tool’s features, such as the godep package manager. If you have some time, I think it’s worth the time investment to learn!

About the author

Nick Maccharoli is an iOS/backend developer and an open source enthusiast working at a start-up in Tokyo and enjoying the current development scene. You can see what he is up to at @din0sr or github.com/nirma.

LEAVE A REPLY

Please enter your comment!
Please enter your name here