How to install Go

The Go programming language has been getting popular for certain types of server-side programming. Installing the compiler and its tools and using them is not as straightforward as you might assume. Here are the steps I use:

  • Go to the Go language website and download the package.

  • Unzip the downloaded file and move it to /usr/local/go:

$ tar xvf go-package-you-downloaded.tar.gz
$ sudo mv go /usr/local
  • Add /usr/local/go/bin to the PATH variable of your shell.

  • Your Go source code and compiled binaries will reside inside a specific directory. Create it and set an environment variable named GOPATH to it:

  • Your source code has to be put under GOPATH in this directory hierarchy: $GOPATH/src/ For example, add the hello.go from this page to $GOPATH/src/ You will need to create that directory hierarchy. If you are getting some Go code from Github, remember to clone it within that $GOPATH/src/ directory.

  • The above source code can be compiled from anywhere using this command:

$ go install

Note how the Go compiler will always look under the $GOPATH/src for the above compilation.

  • After compilation, the binary will be placed at $GOPATH/bin. This directory also needs to be added to the PATH variable. Remember to do this cause when you start using Go code that calls other Go libraries, this is essential for that to work.

  • To run the program you compiled:

$ $GOPATH/bin/hello

Tried with: Go 1.7 and Ubuntu 16.04

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s