2012-02-13 23:02:28 -07:00
<!-- {
"Title": "Installing Go from source",
2012-03-04 20:31:27 -07:00
"Path": "/doc/install/source"
2012-02-13 23:02:28 -07:00
}-->
< h2 id = "introduction" > Introduction< / h2 >
2012-03-02 15:36:09 -07:00
< p >
Go is an open source project, distributed under a
2012-02-13 23:02:28 -07:00
< a href = "/LICENSE" > BSD-style license< / a > .
This document explains how to check out the sources,
build them on your own machine, and run them.
< / p >
2012-03-02 15:36:09 -07:00
< p >
Most users don't need to do this, and will instead install
from precompiled binary packages as described in
2012-03-04 20:31:27 -07:00
< a href = "/doc/install" > Getting Started< / a > ,
2012-03-02 15:36:09 -07:00
a much simpler process.
If you want to help develop what goes into those precompiled
packages, though, read on.
< / p >
2012-02-13 23:02:28 -07:00
< div class = "detail" >
< p >
There are two official Go compiler tool chains.
This document focuses on the < code > gc< / code > Go
compiler and tools (< code > 6g< / code > , < code > 8g< / code > etc.).
2012-03-02 15:36:09 -07:00
For information on how to work on < code > gccgo< / code > , a more traditional
2012-02-13 23:02:28 -07:00
compiler using the GCC back end, see
2012-03-04 20:31:27 -07:00
< a href = "/doc/install/gccgo" > Setting up and using gccgo< / a > .
2012-02-13 23:02:28 -07:00
< / p >
< p >
The Go compilers support three instruction sets.
There are important differences in the quality of the compilers for the different
architectures.
< / p >
< dl >
< dt >
< code > amd64< / code > (a.k.a. < code > x86-64< / code > ); < code > 6g,6l,6c,6a< / code >
< / dt >
< dd >
2012-03-02 15:36:09 -07:00
A mature implementation. The compiler has an effective
2012-02-13 23:02:28 -07:00
optimizer (registerizer) and generates good code (although
< code > gccgo< / code > can do noticeably better sometimes).
< / dd >
< dt >
< code > 386< / code > (a.k.a. < code > x86< / code > or < code > x86-32< / code > ); < code > 8g,8l,8c,8a< / code >
< / dt >
< dd >
Comparable to the < code > amd64< / code > port.
< / dd >
< dt >
< code > arm< / code > (a.k.a. < code > ARM< / code > ); < code > 5g,5l,5c,5a< / code >
< / dt >
< dd >
2013-06-09 09:15:39 -06:00
Supports Linux, FreeBSD and NetBSD binaries. Less widely used than the other ports.
2012-02-13 23:02:28 -07:00
< / dd >
< / dl >
< p >
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.
< / p >
< p >
2014-03-25 23:06:18 -06:00
The compilers can target the DragonFly BSD, FreeBSD, Linux, NetBSD, OpenBSD,
OS X (Darwin), Plan 9, Solaris and Windows operating systems.
2012-02-13 23:02:28 -07:00
The full set of supported combinations is listed in the discussion of
< a href = "#environment" > environment variables< / a > below.
< / p >
< / div >
2015-02-10 18:44:50 -07:00
< h2 id = "go14" > Install Go compiler binaries< / h2 >
2012-02-13 23:02:28 -07:00
< p >
2015-02-10 18:44:50 -07:00
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 < code > $HOME/go1.4< / code > .
(This path may be overridden by setting the < code > GOROOT_BOOTSTRAP< / code >
environment variable.)
< / p >
< p >
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 < a href = "/dl/" > the downloads page< / a > .
< / p >
< p >
Download the zip or tarball of Go 1.4 for your platform and extract it to
< code > $HOME/go1.4< / code > (or your nominated < code > GOROOT_BOOTSTRAP< / code >
location).
2012-02-13 23:02:28 -07:00
< / p >
2014-12-11 19:53:29 -07:00
< h2 id = "git" > Install Git, if needed< / h2 >
2012-02-13 23:02:28 -07:00
< p >
2014-12-11 19:53:29 -07:00
To perform the next step you must have Git installed. (Check that you
have a < code > git< / code > command before proceeding.)
2012-02-13 23:02:28 -07:00
< / p >
< p >
2014-12-11 19:53:29 -07:00
If you do not have a working Git installation,
2012-04-13 21:27:11 -06:00
follow the instructions on the
2014-12-11 19:53:29 -07:00
< a href = "http://git-scm.com/downloads" > Git downloads< / a > page.
2012-02-13 23:02:28 -07:00
< / p >
< h2 id = "fetch" > Fetch the repository< / h2 >
< p > Go will install to a directory named < code > go< / code > .
Change to the directory that will be its parent
and make sure the < code > go< / code > directory does not exist.
2014-12-11 19:53:29 -07:00
Then clone the repository and check out the latest release tag:< / p >
2012-02-13 23:02:28 -07:00
< pre >
2014-12-11 19:53:29 -07:00
$ git clone https://go.googlesource.com/go
$ cd go
$ git checkout go1.4
2012-02-13 23:02:28 -07:00
< / pre >
2014-12-11 19:53:29 -07:00
< h2 id = "head" > (Optional) Switch to the master branch< / h2 >
2012-09-26 12:39:31 -06:00
< p > If you intend to modify the go source code, and
< a href = "/doc/contribute.html" > contribute your changes< / a >
to the project, then move your repository
2014-12-11 19:53:29 -07:00
off the release branch, and onto the master (development) branch.
2012-09-26 12:39:31 -06:00
Otherwise, skip this step.< / p >
< pre >
2014-12-11 19:53:29 -07:00
$ git checkout master
2012-09-26 12:39:31 -06:00
< / pre >
2012-02-13 23:02:28 -07:00
< h2 id = "install" > Install Go< / h2 >
< p >
To build the Go distribution, run
< / p >
< pre >
2015-02-25 11:57:45 -07:00
$ cd src
2012-02-13 23:02:28 -07:00
$ ./all.bash
< / pre >
< p >
(To build under Windows use < code > all.bat< / code > .)
< / p >
< p >
If all goes well, it will finish by printing output like:
< / p >
< pre >
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. ***
< / pre >
< p >
where the details on the last few lines reflect the operating system,
architecture, and root directory used during the install.
< / p >
< div class = "detail" >
< p >
For more information about ways to control the build, see the discussion of
< a href = "#environment" > environment variables< / a > below.
2013-12-15 19:03:12 -07:00
< code > all.bash< / code > (or < code > all.bat< / code > ) 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 < code > make.bash< / code > (or < code > make.bat< / code > )
instead.
2012-02-13 23:02:28 -07:00
< / p >
< / div >
< h2 id = "testing" > Testing your installation< / h2 >
< p >
Check that Go is installed correctly by building a simple program.
< / p >
< p >
Create a file named < code > hello.go< / code > and put the following program in it:
< / p >
< pre >
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
< / pre >
< p >
Then run it with the < code > go< / code > tool:
< / p >
< pre >
$ go run hello.go
hello, world
< / pre >
< p >
If you see the "hello, world" message then Go is installed correctly.
< / p >
2012-06-12 18:33:02 -06:00
< h2 id = "gopath" > Set up your work environment< / h2 >
< p >
2013-11-20 13:55:29 -07:00
You're almost done.
You just need to do a little more setup.
2012-06-12 18:33:02 -06:00
< / p >
2012-02-13 23:02:28 -07:00
2013-11-20 13:55:29 -07:00
< p >
< a href = "/doc/code.html" class = "download" id = "start" >
< span class = "big" > How to Write Go Code< / span >
< span class = "desc" > Learn how to set up and use the Go tools< / span >
< / a >
< / p >
< p >
The < a href = "/doc/code.html" > How to Write Go Code< / a > document
provides < b > essential setup instructions< / b > for using the Go tools.
< / p >
2013-09-24 22:10:49 -06:00
< h2 id = "tools" > Install additional tools< / h2 >
< p >
2013-09-24 22:27:23 -06:00
The source code for several Go tools (including < a href = "/cmd/godoc/" > godoc< / a > )
2014-11-09 15:15:57 -07:00
is kept in < a href = "https://golang.org/x/tools" > the go.tools repository< / a > .
2013-09-24 22:10:49 -06:00
To install all of them, run the < code > go< / code > < code > get< / code > command:
< / p >
< pre >
2014-11-09 15:15:57 -07:00
$ go get golang.org/x/tools/cmd/...
2013-09-24 22:10:49 -06:00
< / pre >
< p >
Or if you just want to install a specific command (< code > godoc< / code > in this case):
< / p >
< pre >
2014-11-09 15:15:57 -07:00
$ go get golang.org/x/tools/cmd/godoc
2013-09-24 22:10:49 -06:00
< / pre >
< p >
To install these tools, the < code > go< / code > < code > get< / code > command requires
2014-12-11 19:53:29 -07:00
that < a href = "#git" > Git< / a > be installed locally.
2013-09-24 22:10:49 -06:00
< / p >
< p >
You must also have a workspace (< code > GOPATH< / code > ) set up;
see < a href = "/doc/code.html" > How to Write Go Code< / a > for the details.
< / p >
2013-09-25 02:15:29 -06:00
< p >
< b > Note< / b > : The < code > go< / code > command will install the < code > godoc< / code >
binary to < code > $GOROOT/bin< / code > (or < code > $GOBIN< / code > ) and the
< code > cover< / code > and < code > vet< / code > binaries to
< code > $GOROOT/pkg/tool/$GOOS_$GOARCH< / code > .
You can access the latter commands with
"< code > go< / code > < code > tool< / code > < code > cover< / code > " and
"< code > go< / code > < code > tool< / code > < code > vet< / code > ".
< / p >
2012-02-13 23:02:28 -07:00
< h2 id = "community" > Community resources< / h2 >
< p >
2012-03-02 15:36:09 -07:00
The usual community resources such as
< code > #go-nuts< / code > on the < a href = "http://freenode.net/" > Freenode< / a > IRC server
and the
2014-07-24 18:28:39 -06:00
< a href = "//groups.google.com/group/golang-nuts" > Go Nuts< / a >
2012-03-02 15:36:09 -07:00
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,
2014-07-24 18:28:39 -06:00
there is another mailing list, < a href = "//groups.google.com/group/golang-checkins" > golang-checkins< / a > ,
2012-03-02 15:36:09 -07:00
that receives a message summarizing each checkin to the Go repository.
2012-02-13 23:02:28 -07:00
< / p >
< p >
2014-12-11 19:53:29 -07:00
Bugs can be reported using the < a href = "//golang.org/issue/new" > Go issue tracker< / a > .
2012-02-13 23:02:28 -07:00
< / p >
< h2 id = "releases" > Keeping up with releases< / h2 >
< p >
2014-12-11 19:53:29 -07:00
New releases are announced on the
2014-07-24 18:28:39 -06:00
< a href = "//groups.google.com/group/golang-announce" > golang-announce< / a >
2012-02-13 23:02:28 -07:00
mailing list.
2014-12-11 19:53:29 -07:00
Each announcement mentions the latest release tag, for instance,
< code > go1.4< / code > .
2012-02-13 23:02:28 -07:00
< / p >
< p >
To update an existing tree to the latest release, you can run:
< / p >
< pre >
$ cd go/src
2014-12-11 19:53:29 -07:00
$ git fetch
$ git checkout < i > < tag> < / i >
2012-02-13 23:02:28 -07:00
$ ./all.bash
< / pre >
2014-12-11 19:53:29 -07:00
Where < code > < tag> < / code > is the version string of the release.
2012-02-13 23:02:28 -07:00
< h2 id = "environment" > Optional environment variables< / h2 >
< p >
The Go compilation environment can be customized by environment variables.
2012-03-02 15:36:09 -07:00
< i > None is required by the build< / i > , but you may wish to set some
2012-02-13 23:02:28 -07:00
to override the defaults.
< / p >
2013-06-09 09:15:39 -06:00
< ul >
< li > < code > $GOROOT< / code >
2012-03-05 21:16:02 -07:00
< p >
The root of the Go tree, often < code > $HOME/go< / code > .
Its value is built into the tree when it is compiled, and
defaults to the parent of the directory where < code > all.bash< / code > was run.
There is no need to set this unless you want to switch between multiple
local copies of the repository.
< / p >
2013-06-09 09:15:39 -06:00
< li > < code > $GOROOT_FINAL< / code >
2012-03-05 21:16:02 -07:00
< p >
The value assumed by installed binaries and scripts when
< code > $GOROOT< / code > is not set explicitly.
It defaults to the value of < code > $GOROOT< / code > .
If you want to build the Go tree in one location
but move it elsewhere after the build, set
< code > $GOROOT_FINAL< / code > to the eventual location.
< / p >
2013-06-09 09:15:39 -06:00
< li > < code > $GOOS< / code > and < code > $GOARCH< / code >
2012-03-05 21:16:02 -07:00
< p >
The name of the target operating system and compilation architecture.
These default to the values of < code > $GOHOSTOS< / code > and
< code > $GOHOSTARCH< / code > respectively (described below).
< p >
Choices for < code > $GOOS< / code > are
2014-03-25 23:06:18 -06:00
< code > darwin< / code > (Mac OS X 10.6 and above), < code > dragonfly< / code > , < code > freebsd< / code > ,
2012-03-05 21:16:02 -07:00
< code > linux< / code > , < code > netbsd< / code > , < code > openbsd< / code > ,
2014-03-25 21:21:28 -06:00
< code > plan9< / code > , < code > solaris< / code > and < code > windows< / code > .
2012-03-05 21:16:02 -07:00
Choices for < code > $GOARCH< / code > are
< code > amd64< / code > (64-bit x86, the most mature port),
< code > 386< / code > (32-bit x86), and < code > arm< / code > (32-bit ARM).
The valid combinations of < code > $GOOS< / code > and < code > $GOARCH< / code > are:
< table cellpadding = "0" >
< tr >
2013-06-09 09:15:39 -06:00
< th width = "50" > < / th > < th align = "left" width = "100" > < code > $GOOS< / code > < / th > < th align = "left" width = "100" > < code > $GOARCH< / code > < / th >
2012-03-05 21:16:02 -07:00
< / tr >
< tr >
< td > < / td > < td > < code > darwin< / code > < / td > < td > < code > 386< / code > < / td >
< / tr >
< tr >
< td > < / td > < td > < code > darwin< / code > < / td > < td > < code > amd64< / code > < / td >
< / tr >
< tr >
2014-03-25 23:06:18 -06:00
< td > < / td > < td > < code > dragonfly< / code > < / td > < td > < code > 386< / code > < / td >
< / tr >
< tr >
< td > < / td > < td > < code > dragonfly< / code > < / td > < td > < code > amd64< / code > < / td >
< / tr >
< tr >
2012-03-05 21:16:02 -07:00
< td > < / td > < td > < code > freebsd< / code > < / td > < td > < code > 386< / code > < / td >
< / tr >
< tr >
< td > < / td > < td > < code > freebsd< / code > < / td > < td > < code > amd64< / code > < / td >
< / tr >
< tr >
2013-06-09 09:15:39 -06:00
< td > < / td > < td > < code > freebsd< / code > < / td > < td > < code > arm< / code > < / td >
< / tr >
< tr >
2012-03-05 21:16:02 -07:00
< td > < / td > < td > < code > linux< / code > < / td > < td > < code > 386< / code > < / td >
< / tr >
< tr >
< td > < / td > < td > < code > linux< / code > < / td > < td > < code > amd64< / code > < / td >
< / tr >
< tr >
< td > < / td > < td > < code > linux< / code > < / td > < td > < code > arm< / code > < / td >
< / tr >
< tr >
< td > < / td > < td > < code > netbsd< / code > < / td > < td > < code > 386< / code > < / td >
< / tr >
< tr >
< td > < / td > < td > < code > netbsd< / code > < / td > < td > < code > amd64< / code > < / td >
< / tr >
< tr >
2013-06-09 09:15:39 -06:00
< td > < / td > < td > < code > netbsd< / code > < / td > < td > < code > arm< / code > < / td >
< / tr >
< tr >
2012-03-05 21:16:02 -07:00
< td > < / td > < td > < code > openbsd< / code > < / td > < td > < code > 386< / code > < / td >
< / tr >
< tr >
< td > < / td > < td > < code > openbsd< / code > < / td > < td > < code > amd64< / code > < / td >
< / tr >
< tr >
< td > < / td > < td > < code > plan9< / code > < / td > < td > < code > 386< / code > < / td >
< / tr >
< tr >
2013-06-09 09:15:39 -06:00
< td > < / td > < td > < code > plan9< / code > < / td > < td > < code > amd64< / code > < / td >
< / tr >
< tr >
2014-03-25 21:21:28 -06:00
< td > < / td > < td > < code > solaris< / code > < / td > < td > < code > amd64< / code > < / td >
< / tr >
< tr >
2012-03-05 21:16:02 -07:00
< td > < / td > < td > < code > windows< / code > < / td > < td > < code > 386< / code > < / td >
< / tr >
< tr >
< td > < / td > < td > < code > windows< / code > < / td > < td > < code > amd64< / code > < / td >
< / tr >
< / table >
2013-06-09 09:15:39 -06:00
< li > < code > $GOHOSTOS< / code > and < code > $GOHOSTARCH< / code >
2012-03-05 21:16:02 -07:00
< p >
The name of the host operating system and compilation architecture.
These default to the local system's operating system and
architecture.
< / p >
< p >
Valid choices are the same as for < code > $GOOS< / code > and
< code > $GOARCH< / code > , listed above.
The specified values must be compatible with the local system.
For example, you should not set < code > $GOHOSTARCH< / code > to
< code > arm< / code > on an x86 system.
< / p >
2013-06-09 09:15:39 -06:00
< li > < code > $GOBIN< / code >
2012-03-05 21:16:02 -07:00
< p >
2012-03-27 09:57:39 -06:00
The location where Go binaries will be installed.
2012-03-05 21:16:02 -07:00
The default is < code > $GOROOT/bin< / code > .
After installing, you will want to arrange to add this
directory to your < code > $PATH< / code > , so you can use the tools.
2012-03-27 09:57:39 -06:00
If < code > $GOBIN< / code > is set, the < a href = "/cmd/go" > go command< / a >
installs all commands there.
2012-03-05 21:16:02 -07:00
< / p >
2013-06-09 09:15:39 -06:00
< li > < code > $GO386< / code > (for < code > 386< / code > only, default is auto-detected
2014-06-04 17:53:37 -06:00
if built on either < code > 386< / code > or < code > amd64< / code > , < code > 387< / code > otherwise)
2013-06-09 09:15:39 -06:00
< p >
This controls the code generated by 8g to use either the 387 floating-point unit
(set to < code > 387< / code > ) or SSE2 instructions (set to < code > sse2< / code > ) for
floating point computations.
< / p >
< ul >
< li > < code > GO386=387< / code > : use x87 for floating point operations; should support all x86 chips (Pentium MMX or later).
< li > < code > GO386=sse2< / code > : use SSE2 for floating point operations; has better performance than 387, but only available on Pentium 4/Opteron/Athlon 64 or later.
< / ul >
< li > < code > $GOARM< / code > (for < code > arm< / code > only; default is auto-detected if building
on the target processor, 6 if not)
< p >
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.
< / p >
< ul >
< li > < code > GOARM=5< / code > : use software floating point; when CPU doesn't have VFP co-processor
< li > < code > GOARM=6< / code > : use VFPv1 only; default if cross compiling; usually ARM11 or better cores (VFPv2 or better is also supported)
< li > < code > GOARM=7< / code > : use VFPv3; usually Cortex-A cores
< / ul >
2012-03-05 21:16:02 -07:00
< p >
2013-06-09 09:15:39 -06:00
If in doubt, leave this variable unset, and adjust it if required
when you first run the Go executable.
2014-07-24 18:28:39 -06:00
The < a href = "//golang.org/wiki/GoArm" > GoARM< / a > page
on the < a href = "//golang.org/wiki" > Go community wiki< / a >
2013-06-09 09:15:39 -06:00
contains further details regarding Go's ARM support.
2012-03-05 21:16:02 -07:00
< / p >
2013-06-09 09:15:39 -06:00
< / ul >
2012-02-13 23:02:28 -07:00
< p >
Note that < code > $GOARCH< / code > and < code > $GOOS< / code > identify the
< em > target< / em > 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 < code > GOARCH< / code > to < code > 386< / code > ,
not < code > amd64< / code > .
< / p >
< p >
If you choose to override the defaults,
set these variables in your shell profile (< code > $HOME/.bashrc< / code > ,
< code > $HOME/.profile< / code > , or equivalent). The settings might look
something like this:
< / p >
< pre >
export GOROOT=$HOME/go
export GOARCH=amd64
export GOOS=linux
< / pre >
2012-03-02 15:36:09 -07:00
< p >
although, to reiterate, none of these variables needs to be set to build,
install, and develop the Go tree.
< / p >