There are many ways to configure the Go development environment on your computer, you can choose any 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. The easiest way to get started.
- Install from source code.
- Popular with developers who are familiar with Unix-like systems.
- Use third-party tools.
- There are many third-party tools and package managers for installing Go, like apt-get in Ubuntu and homebrew for Mac.
In case you want to install more than one version of Go in one computer, you should take a look at the tool called GVM. It is the best tool I've seen so far for achieving this job, otherwise you have to know how to deal with this problem by yourself.
Because some parts of Go are written in Plan 9 C and AT&T assembler, you have to install a C compiler before taking the next step.
On a Mac, once you install the Xcode, you have already have the compiler.
On Unix-like systems, you need to install gcc or a like compiler. For example, using the package manager apt-get included with Ubuntu, one can install the required compilers as follows:
sudo apt-get install gcc libc6-dev
On Windows, you need to install MinGW in order to install gcc. Don't forget to configure environment variables after the installation is finished.( Everything looks like this means it's commented by translator: If you are using 64-bit Windows, you would better install 64-bit version of MinGW )
The Go team uses Mercurial to manage source code, so you need to install this tool in order to download Go source code.
At this point, execute following commands to clone Go source code, and start compiling.( It will clone source code to you current directory, switch your work path before you continue. This may take some time. )
hg clone -u release https://code.google.com/p/go
cd go/src
./all.bash
A successful installation will end with the message "ALL TESTS PASSED."
On Windows, you can achieve the same by running all.bat
.
If you are using Windows, installation package will set environment variables automatically. In Unix-like systems, you need to set these variables manually as follows.( If your Go version is greater than 1.0, you don't have to set $GOBIN, and it will automatically be related to your $GOROOT/bin, which we will talk about in the next section)
export GOROOT=$HOME/go
export GOBIN=$GOROOT/bin
export PATH=$PATH:$GOROOT/bin
If you see the following information on your screen, you're all set.
Figure 1.1 Information after installed from source code
Once you see the usage information of Go, it means you successfully installed Go on your computer. If it says "no such command", check if your $PATH environment variable contains the installation path of Go.
Go has one-click installation packages for every operating system. These packages will install Go in /usr/local/go
(c:\Go
in Windows) as default. Of course you can change it, but you also need to change all the environment variables manually as I showed above.
Our next step depends on your operating system type, so we have to check it before we download the standard installation packages.
If you are using Windows, press Win+R
and then run the command tool, type command systeminfo
and it will show you some useful information just few seconds. Find the line with "system type", if you see "x64-based PC" that means your operating system is 64-bit, 32-bit otherwise.
I strongly recommend downloading the 64-bit version of package if you are Mac users as Go is no longer supports pure 32-bit processors in Mac OS.
Linux users can type uname -a
in the terminal to see system information.
64-bit operating system shows as follows.
<some description> x86_64 x86_64 x86_64 GNU/Linux
// some machines such as Ubuntu 10.04 will show as following
x86_64 GNU/Linux
32-bit operating system shows as follows.
<some description> i686 i686 i386 GNU/Linux
Go to the download page, choose go1.0.3.darwin-386.pkg
for 32-bit systems and go1.0.3.darwin-amd64.pkg
for 64-bit systems. All the way to the end by clicking "next", ~/go/bin
will be added to $PATH after you finished the installation. Now open the terminal and type go
. You should now see the what is displayed in figure 1.1.
Go to the [download page]((http://code.google.com/p/go/downloads/list), choose go1.0.3.linux-386.tar.gz
for 32-bit systems and go1.0.3.linux-amd64.tar.gz
for 64-bit systems. Suppose you want to install Go in path $GO_INSTALL_DIR
, uncompress tar.gz
to the path by command tar zxvf go1.0.3.linux-amd64.tar.gz -C $GO_INSTALL_DIR
. Then set your $PATH export PATH=$PATH:$GO_INSTALL_DIR/go/bin
. Now just open the terminal and type go
. You should now see the what is displayed in figure 1.1.
Go to the [download page]((http://code.google.com/p/go/downloads/list), choose go1.0.3.windows-386.msi
for 32-bit systems and go1.0.3.windows-amd64.msi
for 64-bit systems. All the way to the end by clicking "next", c:/go/bin
will be added to path
. Now just open a command line window and type go
. You should now see the what is displayed in figure 1.1.
GVM is a Go multi-version control tool developed by third-party, like rvm in ruby. It's quite easy to use it. Install gvm by typing following commands in your terminal.
bash < <(curl -s https://raw.github.com/moovweb/gvm/master/binscripts/gvm-installer)
Then we install Go by following commands.
gvm install go1.0.3
gvm use go1.0.3
After the process 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 manage tool commonly used in Mac to manage software. Just type following commands to install Go.
brew install go
- Directory
- Previous section: Go environment configuration
- Next section: $GOPATH and workspace