github.com/huandu/go@v0.0.0-20151114150818-04e615e41150/doc/install-source.html (about)

     1  <!--{
     2  	"Title": "Installing Go from source",
     3  	"Path": "/doc/install/source"
     4  }-->
     5  
     6  <h2 id="introduction">Introduction</h2>
     7  
     8  <p>
     9  Go is an open source project, distributed under a
    10  <a href="/LICENSE">BSD-style license</a>.
    11  This document explains how to check out the sources,
    12  build them on your own machine, and run them.
    13  </p>
    14  
    15  <p>
    16  Most users don't need to do this, and will instead install
    17  from precompiled binary packages as described in
    18  <a href="/doc/install">Getting Started</a>,
    19  a much simpler process.
    20  If you want to help develop what goes into those precompiled
    21  packages, though, read on.
    22  </p>
    23  
    24  <div class="detail">
    25  
    26  <p>
    27  There are two official Go compiler tool chains.
    28  This document focuses on the <code>gc</code> Go
    29  compiler and tools.
    30  For information on how to work on <code>gccgo</code>, a more traditional
    31  compiler using the GCC back end, see
    32  <a href="/doc/install/gccgo">Setting up and using gccgo</a>.
    33  </p>
    34  
    35  <p>
    36  The Go compilers support five instruction sets.
    37  There are important differences in the quality of the compilers for the different
    38  architectures.
    39  </p>
    40  
    41  <dl>
    42  <dt>
    43  	<code>amd64</code> (also known as <code>x86-64</code>)
    44  </dt>
    45  <dd>
    46  	A mature implementation. The compiler has an effective
    47  	optimizer (registerizer) and generates good code (although
    48  	<code>gccgo</code> can do noticeably better sometimes).
    49  </dd>
    50  <dt>
    51  	<code>386</code> (<code>x86</code> or <code>x86-32</code>)
    52  </dt>
    53  <dd>
    54  	Comparable to the <code>amd64</code> port.
    55  </dd>
    56  <dt>
    57  	<code>arm</code> (<code>ARM</code>)
    58  </dt>
    59  <dd>
    60  	Supports Linux, FreeBSD, NetBSD and Darwin binaries. Less widely used than the other ports.
    61  </dd>
    62  <dt>
    63  	<code>arm64</code> (<code>AArch64</code>)
    64  </dt>
    65  <dd>
    66  	Supports Linux and Darwin binaries. New in 1.5 and not as well excercised as other ports.
    67  </dd>
    68  <dt>
    69  	<code>ppc64, ppc64le</code> (64-bit PowerPC big- and little-endian)
    70  </dt>
    71  <dd>
    72  	Supports Linux binaries. New in 1.5 and not as well excercised as other ports.
    73  </dd>
    74  </dl>
    75  
    76  <p>
    77  Except for things like low-level operating system interface code, the run-time
    78  support is the same in all ports and includes a mark-and-sweep garbage
    79  collector, efficient array and string slicing, and support for efficient
    80  goroutines, such as stacks that grow and shrink on demand.
    81  </p>
    82  
    83  <p>
    84  The compilers can target the DragonFly BSD, FreeBSD, Linux, NetBSD, OpenBSD,
    85  OS X (Darwin), Plan 9, Solaris and Windows operating systems.
    86  The full set of supported combinations is listed in the discussion of
    87  <a href="#environment">environment variables</a> below.
    88  </p>
    89  
    90  </div>
    91  
    92  <h2 id="go14">Install Go compiler binaries</h2>
    93  
    94  <p>
    95  The Go tool chain is written in Go. To build it, you need a Go compiler installed.
    96  The scripts that do the initial build of the tools look for an existing Go tool
    97  chain in <code>$HOME/go1.4</code>.
    98  (This path may be overridden by setting the <code>GOROOT_BOOTSTRAP</code>
    99  environment variable.)
   100  </p>
   101  
   102  <p>
   103  Build the tools with Go version 1.4 or a point release (1.4.1, 1.4.2 etc.).
   104  Go 1.4 binaries can be found at <a href="/dl/">the downloads page</a>.
   105  </p>
   106  
   107  <p>
   108  Download the zip or tarball of Go 1.4 for your platform and extract it to
   109  <code>$HOME/go1.4</code> (or your nominated <code>GOROOT_BOOTSTRAP</code>
   110  location).
   111  </p>
   112  
   113  <p>
   114  If you want to install Go 1.5 on a system that is not supported by Go 1.4 (such
   115  as <code>linux/ppc64</code>) you can either use
   116  <a href="/src/bootstrap.bash">bootstrap.bash</a> on a system that can bootstrap Go
   117  1.5 normally, or bootstrap with gccgo 5.
   118  </p>
   119  
   120  <p>
   121  When run as (for example)
   122  </p>
   123  
   124  <pre>
   125  $ GOOS=linux GOARCH=ppc64 ./bootstrap.bash
   126  </pre>
   127  
   128  <p>
   129  <code>bootstrap.bash</code> cross-compiles a toolchain for that <code>GOOS/GOARCH</code>
   130  combination, leaving the resulting tree in <code>../../go-${GOOS}-${GOARCH}-bootstrap</code>.
   131  That tree can be copied to a machine of the given target type
   132  and used as <code>GOROOT_BOOTSTRAP</code> to bootstrap a local build.
   133  </p>
   134  
   135  <p>
   136  To use gccgo, you need to arrange for <code>$GOROOT_BOOSTRAP/bin/go</code> to be
   137  the go tool that comes as part of gccgo 5. For example on Ubuntu Vivid:
   138  </p>
   139  
   140  <pre>
   141  $ sudo apt-get install gccgo-5
   142  $ sudo update-alternatives --set go /usr/bin/go-5
   143  $ GOROOT_BOOTSTRAP=/usr ./make.bash
   144  </pre>
   145  
   146  <h2 id="git">Install Git, if needed</h2>
   147  
   148  <p>
   149  To perform the next step you must have Git installed. (Check that you
   150  have a <code>git</code> command before proceeding.)
   151  </p>
   152  
   153  <p>
   154  If you do not have a working Git installation,
   155  follow the instructions on the
   156  <a href="http://git-scm.com/downloads">Git downloads</a> page.
   157  </p>
   158  
   159  
   160  <h2 id="fetch">Fetch the repository</h2>
   161  
   162  <p>Go will install to a directory named <code>go</code>.
   163  Change to the directory that will be its parent
   164  and make sure the <code>go</code> directory does not exist.
   165  Then clone the repository and check out the latest release tag:</p>
   166  
   167  <pre>
   168  $ git clone https://go.googlesource.com/go
   169  $ cd go
   170  $ git checkout go1.5
   171  </pre>
   172  
   173  <h2 id="head">(Optional) Switch to the master branch</h2>
   174  
   175  <p>If you intend to modify the go source code, and
   176  <a href="/doc/contribute.html">contribute your changes</a>
   177  to the project, then move your repository
   178  off the release branch, and onto the master (development) branch.
   179  Otherwise, skip this step.</p>
   180  
   181  <pre>
   182  $ git checkout master
   183  </pre>
   184  
   185  <h2 id="install">Install Go</h2>
   186  
   187  <p>
   188  To build the Go distribution, run
   189  </p>
   190  
   191  <pre>
   192  $ cd src
   193  $ ./all.bash
   194  </pre>
   195  
   196  <p>
   197  (To build under Windows use <code>all.bat</code>.)
   198  </p>
   199  
   200  <p>
   201  If all goes well, it will finish by printing output like:
   202  </p>
   203  
   204  <pre>
   205  ALL TESTS PASSED
   206  
   207  ---
   208  Installed Go for linux/amd64 in /home/you/go.
   209  Installed commands in /home/you/go/bin.
   210  *** You need to add /home/you/go/bin to your $PATH. ***
   211  </pre>
   212  
   213  <p>
   214  where the details on the last few lines reflect the operating system,
   215  architecture, and root directory used during the install.
   216  </p>
   217  
   218  <div class="detail">
   219  <p>
   220  For more information about ways to control the build, see the discussion of
   221  <a href="#environment">environment variables</a> below.
   222  <code>all.bash</code> (or <code>all.bat</code>) runs important tests for Go,
   223  which can take more time than simply building Go. If you do not want to run
   224  the test suite use <code>make.bash</code> (or <code>make.bat</code>)
   225  instead.
   226  </p>
   227  </div>
   228  
   229  
   230  <h2 id="testing">Testing your installation</h2>
   231  
   232  <p>
   233  Check that Go is installed correctly by building a simple program.
   234  </p>
   235  
   236  <p>
   237  Create a file named <code>hello.go</code> and put the following program in it:
   238  </p>
   239  
   240  <pre>
   241  package main
   242  
   243  import "fmt"
   244  
   245  func main() {
   246      fmt.Printf("hello, world\n")
   247  }
   248  </pre>
   249  
   250  <p>
   251  Then run it with the <code>go</code> tool:
   252  </p>
   253  
   254  <pre>
   255  $ go run hello.go
   256  hello, world
   257  </pre>
   258  
   259  <p>
   260  If you see the "hello, world" message then Go is installed correctly.
   261  </p>
   262  
   263  <h2 id="gopath">Set up your work environment</h2>
   264  
   265  <p>
   266  You're almost done.
   267  You just need to do a little more setup.
   268  </p>
   269  
   270  <p>
   271  <a href="/doc/code.html" class="download" id="start">
   272  <span class="big">How to Write Go Code</span>
   273  <span class="desc">Learn how to set up and use the Go tools</span>
   274  </a>
   275  </p>
   276  
   277  <p>
   278  The <a href="/doc/code.html">How to Write Go Code</a> document 
   279  provides <b>essential setup instructions</b> for using the Go tools.
   280  </p>
   281  
   282  
   283  <h2 id="tools">Install additional tools</h2>
   284  
   285  <p>
   286  The source code for several Go tools (including <a href="/cmd/godoc/">godoc</a>)
   287  is kept in <a href="https://golang.org/x/tools">the go.tools repository</a>.
   288  To install all of them, run the <code>go</code> <code>get</code> command:
   289  </p>
   290  
   291  <pre>
   292  $ go get golang.org/x/tools/cmd/...
   293  </pre>
   294  
   295  <p>
   296  Or if you just want to install a specific command (<code>godoc</code> in this case):
   297  </p>
   298  
   299  <pre>
   300  $ go get golang.org/x/tools/cmd/godoc
   301  </pre>
   302  
   303  <p>
   304  To install these tools, the <code>go</code> <code>get</code> command requires 
   305  that <a href="#git">Git</a> be installed locally.
   306  </p>
   307  
   308  <p>
   309  You must also have a workspace (<code>GOPATH</code>) set up;
   310  see <a href="/doc/code.html">How to Write Go Code</a> for the details.
   311  </p>
   312  
   313  <p>
   314  <b>Note</b>: The <code>go</code> command will install the <code>godoc</code>
   315  binary to <code>$GOROOT/bin</code> (or <code>$GOBIN</code>) and the
   316  <code>cover</code> and <code>vet</code> binaries to
   317  <code>$GOROOT/pkg/tool/$GOOS_$GOARCH</code>.
   318  You can access the latter commands with
   319  "<code>go</code> <code>tool</code> <code>cover</code>" and
   320  "<code>go</code> <code>tool</code> <code>vet</code>".
   321  </p>
   322  
   323  <h2 id="community">Community resources</h2>
   324  
   325  <p>
   326  The usual community resources such as
   327  <code>#go-nuts</code> on the <a href="http://freenode.net/">Freenode</a> IRC server
   328  and the
   329  <a href="//groups.google.com/group/golang-nuts">Go Nuts</a>
   330  mailing list have active developers that can help you with problems
   331  with your installation or your development work.
   332  For those who wish to keep up to date,
   333  there is another mailing list, <a href="//groups.google.com/group/golang-checkins">golang-checkins</a>,
   334  that receives a message summarizing each checkin to the Go repository.
   335  </p>
   336  
   337  <p>
   338  Bugs can be reported using the <a href="//golang.org/issue/new">Go issue tracker</a>.
   339  </p>
   340  
   341  
   342  <h2 id="releases">Keeping up with releases</h2>
   343  
   344  <p>
   345  New releases are announced on the
   346  <a href="//groups.google.com/group/golang-announce">golang-announce</a>
   347  mailing list.
   348  Each announcement mentions the latest release tag, for instance,
   349  <code>go1.5</code>.
   350  </p>
   351  
   352  <p>
   353  To update an existing tree to the latest release, you can run:
   354  </p>
   355  
   356  <pre>
   357  $ cd go/src
   358  $ git fetch
   359  $ git checkout <i>&lt;tag&gt;</i>
   360  $ ./all.bash
   361  </pre>
   362  
   363  Where <code>&lt;tag&gt;</code> is the version string of the release.
   364  
   365  
   366  <h2 id="environment">Optional environment variables</h2>
   367  
   368  <p>
   369  The Go compilation environment can be customized by environment variables.
   370  <i>None is required by the build</i>, but you may wish to set some
   371  to override the defaults.
   372  </p>
   373  
   374  <ul>
   375  <li><code>$GOROOT</code>
   376  <p>
   377  The root of the Go tree, often <code>$HOME/go</code>.
   378  Its value is built into the tree when it is compiled, and
   379  defaults to the parent of the directory where <code>all.bash</code> was run.
   380  There is no need to set this unless you want to switch between multiple
   381  local copies of the repository.
   382  </p>
   383  
   384  <li><code>$GOROOT_FINAL</code>
   385  <p>
   386  The value assumed by installed binaries and scripts when
   387  <code>$GOROOT</code> is not set explicitly.
   388  It defaults to the value of <code>$GOROOT</code>.
   389  If you want to build the Go tree in one location
   390  but move it elsewhere after the build, set 
   391  <code>$GOROOT_FINAL</code> to the eventual location.
   392  </p>
   393  
   394  <li><code>$GOOS</code> and <code>$GOARCH</code>
   395  <p>
   396  The name of the target operating system and compilation architecture.
   397  These default to the values of <code>$GOHOSTOS</code> and
   398  <code>$GOHOSTARCH</code> respectively (described below).
   399  
   400  <p>
   401  Choices for <code>$GOOS</code> are
   402  <code>darwin</code> (Mac OS X 10.7 and above and iOS), <code>dragonfly</code>, <code>freebsd</code>,
   403  <code>linux</code>, <code>netbsd</code>, <code>openbsd</code>,
   404  <code>plan9</code>, <code>solaris</code> and <code>windows</code>.
   405  Choices for <code>$GOARCH</code> are
   406  <code>amd64</code> (64-bit x86, the most mature port),
   407  <code>386</code> (32-bit x86), <code>arm</code> (32-bit ARM), <code>arm64</code> (64-bit ARM),
   408  <code>ppc64le</code> (PowerPC 64-bit, little-endian), and <code>ppc64</code> (PowerPC 64-bit, big-endian).
   409  The valid combinations of <code>$GOOS</code> and <code>$GOARCH</code> are:
   410  <table cellpadding="0">
   411  <tr>
   412  <th width="50"></th><th align="left" width="100"><code>$GOOS</code></th> <th align="left" width="100"><code>$GOARCH</code></th>
   413  </tr>
   414  <tr>
   415  <td></td><td><code>darwin</code></td> <td><code>386</code></td>
   416  </tr>
   417  <tr>
   418  <td></td><td><code>darwin</code></td> <td><code>amd64</code></td>
   419  </tr>
   420  <tr>
   421  <td></td><td><code>darwin</code></td> <td><code>arm</code></td>
   422  </tr>
   423  <tr>
   424  <td></td><td><code>darwin</code></td> <td><code>arm64</code></td>
   425  </tr>
   426  <tr>
   427  <td></td><td><code>dragonfly</code></td> <td><code>amd64</code></td>
   428  </tr>
   429  <tr>
   430  <td></td><td><code>freebsd</code></td> <td><code>386</code></td>
   431  </tr>
   432  <tr>
   433  <td></td><td><code>freebsd</code></td> <td><code>amd64</code></td>
   434  </tr>
   435  <tr>
   436  <td></td><td><code>freebsd</code></td> <td><code>arm</code></td>
   437  </tr>
   438  <tr>
   439  <td></td><td><code>linux</code></td> <td><code>386</code></td>
   440  </tr>
   441  <tr>
   442  <td></td><td><code>linux</code></td> <td><code>amd64</code></td>
   443  </tr>
   444  <tr>
   445  <td></td><td><code>linux</code></td> <td><code>arm</code></td>
   446  </tr>
   447  <tr>
   448  <td></td><td><code>linux</code></td> <td><code>arm64</code></td>
   449  </tr>
   450  <tr>
   451  <td></td><td><code>linux</code></td> <td><code>ppc64</code></td>
   452  </tr>
   453  <tr>
   454  <td></td><td><code>linux</code></td> <td><code>ppc64le</code></td>
   455  </tr>
   456  <tr>
   457  <td></td><td><code>netbsd</code></td> <td><code>386</code></td>
   458  </tr>
   459  <tr>
   460  <td></td><td><code>netbsd</code></td> <td><code>amd64</code></td>
   461  </tr>
   462  <tr>
   463  <td></td><td><code>netbsd</code></td> <td><code>arm</code></td>
   464  </tr>
   465  <tr>
   466  <td></td><td><code>openbsd</code></td> <td><code>386</code></td>
   467  </tr>
   468  <tr>
   469  <td></td><td><code>openbsd</code></td> <td><code>amd64</code></td>
   470  </tr>
   471  <tr>
   472  <td></td><td><code>openbsd</code></td> <td><code>arm</code></td>
   473  </tr>
   474  <tr>
   475  <td></td><td><code>plan9</code></td> <td><code>386</code></td>
   476  </tr>
   477  <tr>
   478  <td></td><td><code>plan9</code></td> <td><code>amd64</code></td>
   479  </tr>
   480  <tr>
   481  <td></td><td><code>solaris</code></td> <td><code>amd64</code></td>
   482  </tr>
   483  <tr>
   484  <td></td><td><code>windows</code></td> <td><code>386</code></td>
   485  </tr>
   486  <tr>
   487  <td></td><td><code>windows</code></td> <td><code>amd64</code></td>
   488  </tr>
   489  </table>
   490  <br>
   491  
   492  <li><code>$GOHOSTOS</code> and <code>$GOHOSTARCH</code>
   493  <p>
   494  The name of the host operating system and compilation architecture.
   495  These default to the local system's operating system and
   496  architecture.
   497  </p>
   498  
   499  <p>
   500  Valid choices are the same as for <code>$GOOS</code> and
   501  <code>$GOARCH</code>, listed above.
   502  The specified values must be compatible with the local system.
   503  For example, you should not set <code>$GOHOSTARCH</code> to 
   504  <code>arm</code> on an x86 system.
   505  </p>
   506  
   507  <li><code>$GOBIN</code>
   508  <p>
   509  The location where Go binaries will be installed.
   510  The default is <code>$GOROOT/bin</code>.
   511  After installing, you will want to arrange to add this
   512  directory to your <code>$PATH</code>, so you can use the tools.
   513  If <code>$GOBIN</code> is set, the <a href="/cmd/go">go command</a>
   514  installs all commands there.
   515  </p>
   516  
   517  <li><code>$GO386</code> (for <code>386</code> only, default is auto-detected
   518  if built on either <code>386</code> or <code>amd64</code>, <code>387</code> otherwise)
   519  <p>
   520  This controls the code generated by gc to use either the 387 floating-point unit
   521  (set to <code>387</code>) or SSE2 instructions (set to <code>sse2</code>) for
   522  floating point computations.
   523  </p>
   524  <ul>
   525  	<li><code>GO386=387</code>: use x87 for floating point operations; should support all x86 chips (Pentium MMX or later).
   526  	<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.
   527  </ul>
   528  
   529  <li><code>$GOARM</code> (for <code>arm</code> only; default is auto-detected if building
   530  on the target processor, 6 if not)
   531  <p>
   532  This sets the ARM floating point co-processor architecture version the run-time
   533  should target. If you are compiling on the target system, its value will be auto-detected.
   534  </p>
   535  <ul>
   536  	<li><code>GOARM=5</code>: use software floating point; when CPU doesn't have VFP co-processor
   537  	<li><code>GOARM=6</code>: use VFPv1 only; default if cross compiling; usually ARM11 or better cores (VFPv2 or better is also supported)
   538  	<li><code>GOARM=7</code>: use VFPv3; usually Cortex-A cores
   539  </ul>
   540  <p>
   541  If in doubt, leave this variable unset, and adjust it if required
   542  when you first run the Go executable.
   543  The <a href="//golang.org/wiki/GoArm">GoARM</a> page
   544  on the <a href="//golang.org/wiki">Go community wiki</a>
   545  contains further details regarding Go's ARM support.
   546  </p>
   547  
   548  </ul>
   549  
   550  <p>
   551  Note that <code>$GOARCH</code> and <code>$GOOS</code> identify the
   552  <em>target</em> environment, not the environment you are running on.
   553  In effect, you are always cross-compiling.
   554  By architecture, we mean the kind of binaries
   555  that the target environment can run:
   556  an x86-64 system running a 32-bit-only operating system
   557  must set <code>GOARCH</code> to <code>386</code>,
   558  not <code>amd64</code>.
   559  </p>
   560  
   561  <p>
   562  If you choose to override the defaults,
   563  set these variables in your shell profile (<code>$HOME/.bashrc</code>,
   564  <code>$HOME/.profile</code>, or equivalent). The settings might look 
   565  something like this:
   566  </p>
   567  
   568  <pre>
   569  export GOROOT=$HOME/go
   570  export GOARCH=amd64
   571  export GOOS=linux
   572  </pre>
   573  
   574  <p>
   575  although, to reiterate, none of these variables needs to be set to build,
   576  install, and develop the Go tree.
   577  </p>