OxDEAD Unicornz

Have you ever seen so many?

Few Notes on Local Golang Environment Configuration

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:

1
2
3
4
5
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

1
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:

1
2
3
4
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:

1
gofmt onetimer.go

golint

Golint is a linter for Go source code.

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

Usage example:

1
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.

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