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.
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 five instruction sets. There are important differences in the quality of the compilers for the different architectures.
amd64
(also known as x86-64
)
gccgo
can do noticeably better sometimes).
386
(x86
or x86-32
)
amd64
port.
arm
(ARM
)
arm64
(AArch64
)
ppc64, ppc64le
(64-bit PowerPC big- and little-endian)
mips64, mips64le
(64-bit MIPS big- and little-endian)
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 DragonFly BSD, FreeBSD, Linux, NetBSD, OpenBSD, OS X (Darwin), Plan 9, Solaris 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 Go. To build it, you need a Go compiler installed.
The scripts that do the initial build of the tools look for an existing Go tool
chain in $HOME/go1.4
.
(This path may be overridden by setting the GOROOT_BOOTSTRAP
environment variable.)
Build the tools with Go version 1.4 or a point release (1.4.1, 1.4.2 etc.). Go 1.4 binaries can be found at the downloads page.
Download the zip or tarball of Go 1.4 for your platform and extract it to
$HOME/go1.4
(or your nominated GOROOT_BOOTSTRAP
location).
If you want to install Go 1.5 on a system that is not supported by Go 1.4 (such
as linux/ppc64
and linux/mips64le
) you can either use
bootstrap.bash on a system that can bootstrap Go
1.5 normally, or bootstrap with gccgo 5.
When run as (for example)
$ GOOS=linux GOARCH=ppc64 ./bootstrap.bash
bootstrap.bash
cross-compiles a toolchain for that GOOS/GOARCH
combination, leaving the resulting tree in ../../go-${GOOS}-${GOARCH}-bootstrap
.
That tree can be copied to a machine of the given target type
and used as GOROOT_BOOTSTRAP
to bootstrap a local build.
To use gccgo, you need to arrange for $GOROOT_BOOSTRAP/bin/go
to be
the go tool that comes as part of gccgo 5. For example on Ubuntu Vivid:
$ sudo apt-get install gccgo-5 $ sudo update-alternatives --set go /usr/bin/go-5 $ GOROOT_BOOTSTRAP=/usr ./make.bash
To perform the next step you must have Git installed. (Check that you
have a git
command before proceeding.)
If you do not have a working Git installation, follow the instructions on the Git downloads page.
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 clone the repository and check out the latest release tag:
$ git clone https://go.googlesource.com/go $ cd go $ git checkout go1.5.2
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 master (development) branch. Otherwise, skip this step.
$ git checkout master
To build the Go distribution, run
$ cd 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.
all.bash
(or all.bat
) runs important tests for Go,
which can take more time than simply building Go. If you do not want to run
the test suite use make.bash
(or make.bat
)
instead.
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.
You're almost done. You just need to do a little more setup.
How to Write Go Code Learn how to set up and use the Go tools
The How to Write Go Code document provides essential setup instructions for using the Go tools.
The source code for several Go tools (including godoc)
is kept in the go.tools repository.
To install all of them, run the go
get
command:
$ go get golang.org/x/tools/cmd/...
Or if you just want to install a specific command (godoc
in this case):
$ go get golang.org/x/tools/cmd/godoc
To install these tools, the go
get
command requires
that Git be installed locally.
You must also have a workspace (GOPATH
) set up;
see How to Write Go Code for the details.
Note: The go
command will install the godoc
binary to $GOROOT/bin
(or $GOBIN
) and the
cover
and vet
binaries to
$GOROOT/pkg/tool/$GOOS_$GOARCH
.
You can access the latter commands with
"go
tool
cover
" and
"go
tool
vet
".
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.
New releases are announced on the
golang-announce
mailing list.
Each announcement mentions the latest release tag, for instance,
go1.5.2
.
To update an existing tree to the latest release, you can run:
$ cd go/src $ git fetch $ git checkout <tag> $ ./all.bashWhere
<tag>
is the version string of the release.
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 where all.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
are
darwin
(Mac OS X 10.7 and above and iOS), dragonfly
, freebsd
,
linux
, netbsd
, openbsd
,
plan9
, solaris
and windows
.
Choices for $GOARCH
are
amd64
(64-bit x86, the most mature port),
386
(32-bit x86), arm
(32-bit ARM), arm64
(64-bit ARM),
ppc64le
(PowerPC 64-bit, little-endian), ppc64
(PowerPC 64-bit, big-endian),
mips64le
(MIPS 64-bit, little-endian), and mips64
(MIPS 64-bit, big-endian).
The valid combinations of $GOOS
and $GOARCH
are:
$GOOS | $GOARCH |
|
---|---|---|
darwin | 386 |
|
darwin | amd64 |
|
darwin | arm |
|
darwin | arm64 |
|
dragonfly | amd64 |
|
freebsd | 386 |
|
freebsd | amd64 |
|
freebsd | arm |
|
linux | 386 |
|
linux | amd64 |
|
linux | arm |
|
linux | arm64 |
|
linux | ppc64 |
|
linux | ppc64le |
|
linux | mips64 |
|
linux | mips64le |
|
netbsd | 386 |
|
netbsd | amd64 |
|
netbsd | arm |
|
openbsd | 386 |
|
openbsd | amd64 |
|
openbsd | arm |
|
plan9 | 386 |
|
plan9 | amd64 |
|
solaris | amd64 |
|
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
to
arm
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.
$GO386
(for 386
only, default is auto-detected
if built on either 386
or amd64
, 387
otherwise)
This controls the code generated by gc to use either the 387 floating-point unit
(set to 387
) or SSE2 instructions (set to sse2
) for
floating point computations.
GO386=387
: use x87 for floating point operations; should support all x86 chips (Pentium MMX or later).
GO386=sse2
: use SSE2 for floating point operations; has better performance than 387, but only available on Pentium 4/Opteron/Athlon 64 or later.
$GOARM
(for arm
only; default is auto-detected if building
on the target processor, 6 if not)
This sets the ARM floating point co-processor architecture version the run-time should target. If you are compiling on the target system, its value will be auto-detected.
GOARM=5
: use software floating point; when CPU doesn't have VFP co-processor
GOARM=6
: use VFPv1 only; default if cross compiling; usually ARM11 or better cores (VFPv2 or better is also supported)
GOARM=7
: use VFPv3; usually Cortex-A cores
If in doubt, leave this variable unset, and adjust it if required when you first run the Go executable. The GoARM page on the Go community wiki contains further details regarding Go's ARM 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.