Go is an open source project, distributed under a BSD-style license. This document explains how to check out the sources, build them on your own machine, and run them.
Most users don't need to do this, and will instead install from precompiled binary packages as described in Getting Started, a much simpler process. If you want to help develop what goes into those precompiled packages, though, read on.
There are two official Go compiler tool chains.
This document focuses on the gc
Go
compiler and tools (6g
, 8g
etc.).
For information on how to work on gccgo
, a more traditional
compiler using the GCC back end, see
Setting up and using gccgo.
The Go compilers support three instruction sets. There are important differences in the quality of the compilers for the different architectures.
amd64
(a.k.a. x86-64
); 6g,6l,6c,6a
gccgo
can do noticeably better sometimes).
386
(a.k.a. x86
or x86-32
); 8g,8l,8c,8a
amd64
port.
arm
(a.k.a. ARM
); 5g,5l,5c,5a
Except for things like low-level operating system interface code, the run-time support is the same in all ports and includes a mark-and-sweep garbage collector, efficient array and string slicing, and support for efficient goroutines, such as stacks that grow and shrink on demand.
The compilers can target the FreeBSD, Linux, NetBSD, OpenBSD, OS X (Darwin), and Windows operating systems. The full set of supported combinations is listed in the discussion of environment variables below.
The Go tool chain is written in C. To build it, you need a C compiler installed.
On OS X, a C compiler is bundled in the command line tools for
Xcode,
and you don't need to install the whole Xcode to compile Go.
If you have already installed Xcode 4.3+, you can install command
line tools from the Components tab of the Downloads preferences panel.
To verify you have a working compiler, just invoke gcc
in a freshly created Terminal window, unless you see the
"gcc: command not found
" error, you are ready to go.
On Ubuntu/Debian, use sudo apt-get install gcc libc6-dev
.
If you want to build 32-bit binaries on a 64-bit system you'll also need the
libc6-dev-i386
package.
On Windows, install gcc
with
MinGW.
(Make sure you add its bin
subdirectory to your PATH
.)
To perform the next step you must have Mercurial installed. (Check that you
have an hg
command.)
If you do not have a working Mercurial installation, follow the instructions on the Mercurial downloads page.
Mercurial versions 1.7.x and up require the configuration of Certification Authorities (CAs). Error messages of the form:
warning: code.google.com certificate with fingerprint b1:af: ... bc not verified (check hostfingerprints or web.cacerts config setting)
when using Mercurial indicate that the CAs are missing.
Check your Mercurial version (hg --version
) and
configure the CAs
if necessary.
Go will install to a directory named go
.
Change to the directory that will be its parent
and make sure the go
directory does not exist.
Then check out the repository:
$ hg clone -u release https://code.google.com/p/go
If you intend to modify the go source code, and contribute your changes to the project, then move your repository off the release branch, and onto the default (development) branch. Otherwise, skip this step.
$ hg update default
To build the Go distribution, run
$ cd go/src $ ./all.bash
(To build under Windows use all.bat
.)
If all goes well, it will finish by printing output like:
ALL TESTS PASSED --- Installed Go for linux/amd64 in /home/you/go. Installed commands in /home/you/go/bin. *** You need to add /home/you/go/bin to your $PATH. ***
where the details on the last few lines reflect the operating system, architecture, and root directory used during the install.
For more information about ways to control the build, see the discussion of environment variables below.
Check that Go is installed correctly by building a simple program.
Create a file named hello.go
and put the following program in it:
package main import "fmt" func main() { fmt.Printf("hello, world\n") }
Then run it with the go
tool:
$ go run hello.go hello, world
If you see the "hello, world" message then Go is installed correctly.
The document How to Write Go Code explains how to set up a work environment in which to build and test Go code.
The usual community resources such as
#go-nuts
on the Freenode IRC server
and the
Go Nuts
mailing list have active developers that can help you with problems
with your installation or your development work.
For those who wish to keep up to date,
there is another mailing list, golang-checkins,
that receives a message summarizing each checkin to the Go repository.
Bugs can be reported using the Go issue tracker.
The Go project maintains a stable tag in its Mercurial repository:
release
.
The release
tag refers to the current stable release of Go.
Most Go users should use this version. New releases are announced on the
golang-announce
mailing list.
To update an existing tree to the latest release, you can run:
$ cd go/src $ hg pull $ hg update release $ ./all.bash
The Go compilation environment can be customized by environment variables. None is required by the build, but you may wish to set some to override the defaults.
$GOROOT
The root of the Go tree, often
$HOME/go
. Its value is built into the tree when it is compiled, and defaults to the parent of the directory whereall.bash
was run. There is no need to set this unless you want to switch between multiple local copies of the repository.
$GOROOT_FINAL
The value assumed by installed binaries and scripts when
$GOROOT
is not set explicitly. It defaults to the value of$GOROOT
. If you want to build the Go tree in one location but move it elsewhere after the build, set$GOROOT_FINAL
to the eventual location.
$GOOS
and$GOARCH
The name of the target operating system and compilation architecture. These default to the values of
$GOHOSTOS
and$GOHOSTARCH
respectively (described below).Choices for
$GOOS
aredarwin
(Mac OS X 10.6 and above),freebsd
,linux
,netbsd
,openbsd
,plan9
, andwindows
. Choices for$GOARCH
areamd64
(64-bit x86, the most mature port),386
(32-bit x86), andarm
(32-bit ARM). The valid combinations of$GOOS
and$GOARCH
are:
$GOOS
$GOARCH
darwin
386
darwin
amd64
freebsd
386
freebsd
amd64
linux
386
linux
amd64
linux
arm
netbsd
386
netbsd
amd64
openbsd
386
openbsd
amd64
plan9
386
windows
386
windows
amd64
$GOHOSTOS
and$GOHOSTARCH
The name of the host operating system and compilation architecture. These default to the local system's operating system and architecture.
Valid choices are the same as for
$GOOS
and$GOARCH
, listed above. The specified values must be compatible with the local system. For example, you should not set$GOHOSTARCH
toarm
on an x86 system.
$GOBIN
The location where Go binaries will be installed. The default is
$GOROOT/bin
. After installing, you will want to arrange to add this directory to your$PATH
, so you can use the tools. If$GOBIN
is set, the go command installs all commands there.
$GOARM
(arm, default=6)The ARM architecture version the run-time libraries should target. Setting
$GOARM
to 5 causes the linker to emit calls to a software floating point implementation instead of using hardware floating point support.
Note that $GOARCH
and $GOOS
identify the
target environment, not the environment you are running on.
In effect, you are always cross-compiling.
By architecture, we mean the kind of binaries
that the target environment can run:
an x86-64 system running a 32-bit-only operating system
must set GOARCH
to 386
,
not amd64
.
If you choose to override the defaults,
set these variables in your shell profile ($HOME/.bashrc
,
$HOME/.profile
, or equivalent). The settings might look
something like this:
export GOROOT=$HOME/go export GOARCH=amd64 export GOOS=linux
although, to reiterate, none of these variables needs to be set to build, install, and develop the Go tree.