Golang code comprehension tools

Software engineers spend a greater part of time reading and understanding programs. Indeed, most of the time it takes to develop a program is spent reading it and making logical conclusion about what it does.

Go programmers are no exception. Thanks to gofmt they should not worry about source code formatting. The machines are better suited to analyse source code and accomplish comprehension tasks than us.

In this article we will explore several of Go comprehension tools that are responsible for locating definitions, ascertaining types of expressions, deducing implementation relation, computing method sets, finding callers/callees, jumping through channels, understanding aliasing.

Oracle

The oralce is a source analysis tool that answers question about your Go source code. It is installed by executing this command:

$ go get golang.org/x/tools/cmd/oracle

A -pos flag is always required. It defines the current cursor position that should be analysed. The expected value is a bytes offset from the beginning of the file.

These are the subcommands that determines the query to perform:

The -format flag set the output format to:

Overall, the tools provides an friendly interface for adopting it in different development editor: Vim, Emacs, Atom, IntelliJ and etc.

Let see oracle in action:

Pythia

Pythia is a browser based user interface for oracle. It is based on the following packages:

Pythia is installed with the following command:

$ go get github.com/fzipp/pythia

The pythia command tool now can be found in $GOPATH/bin/pythia. A specific package can be opened with the following command:

$ pythia net/http 

By default the command opens your default browser:

Pythia in Action

This can be disabled with -open flag:

$ pythia -open=false net/http

The default listening port is :8080. It can be changed via -http flag:

$ pythia -http :9876 net/http

The packages can be filtered out on their build tags:

$ pythia -tags=unix net/http

Godepgraph

Godepgraph is a program for generating a dependency graph of Go packages. Use go get command to install it:

$ go get github.com/kisielk/godepgraph

Usually the tool is combined with dot command (graphviz CLI):

// this command will generate dependency graph as svg image
// -s ingores the builtin packages
// -horizontal layout the graphics horizontally
$ godepgraph -s -horizontal github.com/codegangsta/gin | dot -Tsvg -o gin-godepgraph.svg

The command above will produce the following graphics:

Godepgraph in Action

The godepgraph tools can be controlled with these flags:

Conclusion

Even though Golang does not have dedicated development environment, software engineers can be very product by adopting some of the comprehensive tools mentioned in this article.

Tags# ,