Few Notes on Local Golang Environment Configuration

Few Notes on Local Golang Environment Configuration
Page content

Have Ubuntu 14.04 installed on the VM I use as a programming/testing environment.

Golang packages in Ubuntu 14.04 repo are a bit old:

dpkg -l | grep golang
ii  golang                                 2:1.3.3-1ubuntu4~ubuntu14.04.1   all          Go programming language compiler - metapackage
ii  golang-doc                             2:1.3.3-1ubuntu4~ubuntu14.04.1   all          Go programming language compiler - documentation
ii  golang-go                              2:1.3.3-1ubuntu4~ubuntu14.04.1   amd64        Go programming language comp
...

Google App Engine SDK comes with Go 1.6.3 as of January 2017. In case if you need some flexibility there is a Golang version manager. Works very similar to rvm for Ruby. Installation is as simple as

bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)

In order to compile Go 1.5+, make sure Go 1.4 is installed first. Go 1.5+ removed the C compilers from the toolchain and replaced them with one written in Go. Obviously, this creates a bootstrapping problem if you don’t already have a working Go install. In order to install 1.6.3:

gvm install go1.4 -B
gvm use go1.4
export GOROOT_BOOTSTRAP=$GOROOT
gvm install go1.6.3

gofmt, golint, govet

A handful of useful tools to keep the code neat and correct.

gofmt

Gofmt formats Go programs. It uses tabs (width = 8) for indentation and blanks for alignment.

Without an explicit path, it processes the standard input. Given a file, it operates on that file; given a directory, it operates on all .go files in that directory, recursively. (Files starting with a period are ignored.) By default, gofmt prints the reformatted sources to standard output.

Gofmt comes with vanilla Golang so no installation required. Example usage:

gofmt onetimer.go

golint

Golint is a linter for Go source code.

go get -u github.com/golang/lint/golint

Usage example:

cd <project_dir> && golint ./...

Note ... wildcard

govet

Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string. Vet uses heuristics that do not guarantee all reports are genuine problems, but it can find error s not caught by the compilers. Comes out of box as well.

cd <project_dir> && go vet ./...