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.
There are two distinct ways to experiment with Go.
This document focuses on the gc
Go
compiler and tools (6g
, 8g
etc.).
For information on how to use 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 runtime support is the same in all ports and includes a mark-and-sweep garbage collector (a fancier one is in the works), efficient array and string slicing, support for segmented stacks, and a strong goroutine implementation.
The compilers can target the FreeBSD, Linux, and OS X (a.k.a. Darwin) operating systems. (A port to Microsoft Windows is in progress but incomplete. See the Windows Port page for details.) 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 these programs installed:
On OS X, they can be installed as part of Xcode.
On Ubuntu/Debian, use sudo apt-get install bison ed gawk gcc libc6-dev make
.
To perform the next step you must have Mercurial installed. (Check that you have an hg
command.) This suffices to install Mercurial on most systems:
sudo easy_install mercurial(On Ubuntu/Debian, you might try
apt-get install python-setuptools
python-dev build-essential
first. The Mercurial in your distribution's
package repository will most likely be old and broken.)
If that fails, try installing manually from the Mercurial Download page.
Mercurial versions 1.7.x and up require the configuration of Certification Authorities (CAs). Error messages of the form:
warning: go.googlecode.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 -r release https://go.googlecode.com/hg/ go
To build the Go distribution, run
$ cd go/src $ ./all.bash
If all goes well, it will finish by printing output like:
--- cd ../test N known bugs; 0 unexpected bugs --- 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. *** The compiler is 6g.
where N is a number that varies from release to release and the details on the last few lines will 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.
Given a file file.go
, compile it using
$ 6g file.go
6g
is the Go compiler for amd64
; it will write the output
in file.6
. The ‘6
’ identifies
files for the amd64
architecture.
The identifier letters for 386
and arm
are ‘8
’ and ‘5
’.
That is, if you were compiling for 386
, you would use
8g
and the output would be named file.8
.
To link the file, use
$ 6l file.6
and to run it
$ ./6.out
A complete example:
$ cat >hello.go <<EOF package main import "fmt" func main() { fmt.Printf("hello, world\n") } EOF $ 6g hello.go $ 6l hello.6 $ ./6.out hello, world $
There is no need to list hello.6
's package dependencies
(in this case, package fmt
) on the 6l
command line.
The linker learns about them by reading hello.6
.
To build more complicated programs, you will probably
want to use a
Makefile
.
There are examples in places like
go/src/cmd/godoc/Makefile
and go/src/pkg/*/Makefile
.
The
document
about contributing to the Go project
gives more detail about
the process of building and testing Go programs.
Start by reading the Go Tutorial.
Build a web application by following the Wiki Codelab.
Read Effective Go to learn about writing idiomatic Go code.
For the full story, consult Go's extensive documentation.
New releases are announced on the Go Nuts mailing list. To update an existing tree to the latest release, you can run:
$ cd go/src $ hg pull $ hg update release $ ./all.bash
For real-time help, there may be users or developers on
#go-nuts
on the Freenode IRC server.
The official mailing list for discussion of the Go language is Go Nuts.
Bugs can be reported using the Go issue tracker.
For those who wish to keep up with development, there is another mailing list, golang-checkins, that receives a message summarizing each checkin to the Go repository.
The Go compilation environment can be customized by environment variables. None are required by the build, but you may wish to set them to override the defaults.
$GOROOT
$HOME/go
.
This defaults to the parent of the directory where all.bash
is run.
If you choose not to set $GOROOT
, you must
run gomake
instead of make
or gmake
when developing Go programs using the conventional makefiles.
$GOROOT_FINAL
$GOROOT
is not set.
It defaults to the value used for $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
$GOHOSTOS
and
$GOHOSTARCH
respectively (described below).
Choices for $GOOS
are linux
,
freebsd
,
darwin
(Mac OS X 10.5 or 10.6),
and windows
(Windows, an incomplete port).
Choices for $GOARCH
are amd64
(64-bit x86, the most mature port),
386
(32-bit x86), and
arm
(32-bit ARM, an incomplete port).
The valid combinations of $GOOS
and $GOARCH
are:
$GOOS | $GOARCH | ||
---|---|---|---|
darwin | 386 |
||
darwin | amd64 |
||
freebsd | 386 |
||
freebsd | amd64 |
||
linux | 386 |
||
linux | amd64 |
||
linux | arm | incomplete | |
windows | 386 | incomplete |
$GOHOSTOS
and $GOHOSTARCH
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
$GOROOT/bin
.
After installing, you will want to arrange to add this
directory to your $PATH
, so you can use the tools.
$GOARM
(arm, default=6)
$GOARM
to 5 will compile the runtime libraries using
just SWP instructions that work on older architectures as well.
Running v6 code on an older core will cause an illegal instruction trap.
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=386 export GOOS=linux