There are many ways to configure the Go development environment on your computer, and you can choose whichever one you like. The three most common ways are as follows.
Official installation packages:
The Go team provides convenient installation packages in Windows, Linux, Mac and other operating systems. This is probably the easiest way to get started.
Install it yourself from source code:
Popular with developers who are familiar with Unix-like systems.
Using third-party tools:
There are many third-party tools and package managers for installing Go, like apt-get in Ubuntu and homebrew for Mac.
1. Install from source code
a) On Unix-like systems, you need to install gcc or a similar compiler. For example, using the package manager apt-get (included with Ubuntu), one can install the required compilers as follows:
sudo apt-get install bison ed gawk gcc libc6-dev make
b) The Go team uses Mercurial to manage their source code, so you need to install this tool in order to download the Go source code.
sudo apt-get install python-setuptools python-dev build-essential
sudo apt-get install mercurial
c) Go will install to a directory named 'go'. This directory should not exist at $GOROOT. Checkout and get the latest code by typing the below command:
hg clone -u release https://code.google.com/p/go
d) Now compile Go source code.
The building and testing take some time (a few minutes) and after successful of all the
tests, the following message appears:
ALL TESTS PASSED
Installed Go for linux/amd64 in /home/ubuntu/go.
Installed commands in /home/ubuntu/go/bin.
*** You need to add /home/ubuntu/go/bin to your $PATH. ***
The compiler is 6g.
e) Verify the installed Go version:
f) Set Go-Environment variables
Now we are ready to set up our workspace. The $GOPATH is a folder (or set of folders) specified by its environment variable. We must notice that this is not the $GOROOT directory where Go is installed.
echo "export GOROOT=\$HOME/go" >> ~/.profile
echo "export GOPATH=$HOME/gocode" >> ~/.profile
echo "PATH=$PATH:\$GOROOT/bin" >> ~/.profile
echo "PATH=$PATH:$GOPATH/bin" >> ~/.profile
We used ~/gocode path in our computer to store the source of our application and its dependencies. The GOPATH directory will also store the binaries of their packages.
2. Using the standard installation packages
Go has one-click installation packages for every supported operating system (Mac, Linux, Windows). These packages will install Go in /usr/local/go (c:\Go in Windows) by default. Of course this can be modified, but you also need to change all the environment variables manually as I've shown above.
3. Use third-party tools
GVM is a Go multi-version control tool developed by a third-party, like rvm for ruby. It's quite easy to use. Install gvm by typing the following commands in your terminal:
bash < <(curl -s -S -L https://raw.github.com/moovweb/gvm/master/binscripts/gvm-installer)
Then we install Go using the following commands:
gvm install go1.0.3
gvm use go1.0.3
After the process has finished, you're all set.
Ubuntu is the most popular desktop release version of Linux. It uses apt-get to manage packages. We can install Go using the following commands.
sudo add-apt-repository ppa:gophers/go
sudo apt-get update
sudo apt-get install golang-stable
Homebrew is a software management tool commonly used in Mac to manage packages. Just type the following commands to install Go.
brew install go