github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/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 (<code>6g</code>, <code>8g</code> etc.).
    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 three 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> (a.k.a. <code>x86-64</code>); <code>6g,6l,6c,6a</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> (a.k.a. <code>x86</code> or <code>x86-32</code>); <code>8g,8l,8c,8a</code>
    52  </dt>
    53  <dd>
    54  	Comparable to the <code>amd64</code> port.
    55  </dd>
    56  <dt>
    57  	<code>arm</code> (a.k.a. <code>ARM</code>); <code>5g,5l,5c,5a</code>
    58  </dt>
    59  <dd>
    60  	Supports only Linux binaries. Less widely used than the other ports and therefore not as thoroughly tested.
    61  </dd>
    62  </dl>
    63  
    64  <p>
    65  Except for things like low-level operating system interface code, the run-time
    66  support is the same in all ports and includes a mark-and-sweep garbage
    67  collector, efficient array and string slicing, and support for efficient
    68  goroutines, such as stacks that grow and shrink on demand.
    69  </p>
    70  
    71  <p>
    72  The compilers can target the FreeBSD, Linux, NetBSD, OpenBSD, OS X (Darwin),
    73  and Windows operating systems.
    74  The full set of supported combinations is listed in the discussion of
    75  <a href="#environment">environment variables</a> below.
    76  </p>
    77  
    78  </div>
    79  
    80  <h2 id="ctools">Install C tools, if needed</h2>
    81  
    82  <p>
    83  The Go tool chain is written in C. To build it, you need a C compiler installed. 
    84  Please refer to the <a href="http://code.google.com/p/go-wiki/wiki/InstallFromSource#Install_C_tools">InstallFromSource</a>
    85  page on the Go community Wiki for operating system specific instructions.
    86  </p>
    87  
    88  <h2 id="mercurial">Install Mercurial, if needed</h2>
    89  
    90  <p>
    91  To perform the next step you must have Mercurial installed. (Check that you
    92  have an <code>hg</code> command.)
    93  </p>
    94  
    95  <p>
    96  If you do not have a working Mercurial installation,
    97  follow the instructions on the
    98  <a href="http://mercurial.selenic.com/downloads/">Mercurial downloads</a> page.
    99  </p>
   100  
   101  <p>
   102  Mercurial versions 1.7.x and up require the configuration of
   103  <a href="http://mercurial.selenic.com/wiki/CACertificates">Certification Authorities</a>
   104  (CAs). Error messages of the form:
   105  </p>
   106  
   107  <pre>
   108  warning: code.google.com certificate with fingerprint b1:af: ... bc not verified (check hostfingerprints or web.cacerts config setting)
   109  </pre>
   110  
   111  <p>
   112  when using Mercurial indicate that the CAs are missing.
   113  Check your Mercurial version (<code>hg --version</code>) and
   114  <a href="http://mercurial.selenic.com/wiki/CACertificates#Configuration_of_HTTPS_certificate_authorities">configure the CAs</a>
   115  if necessary.
   116  </p>
   117  
   118  
   119  <h2 id="fetch">Fetch the repository</h2>
   120  
   121  <p>Go will install to a directory named <code>go</code>.
   122  Change to the directory that will be its parent
   123  and make sure the <code>go</code> directory does not exist.
   124  Then check out the repository:</p>
   125  
   126  <pre>
   127  $ hg clone -u release https://code.google.com/p/go
   128  </pre>
   129  
   130  <h2 id="head">(Optional) Switch to the default branch</h2>
   131  
   132  <p>If you intend to modify the go source code, and
   133  <a href="/doc/contribute.html">contribute your changes</a>
   134  to the project, then move your repository
   135  off the release branch, and onto the default (development) branch.
   136  Otherwise, skip this step.</p>
   137  
   138  <pre>
   139  $ hg update default
   140  </pre>
   141  
   142  <h2 id="install">Install Go</h2>
   143  
   144  <p>
   145  To build the Go distribution, run
   146  </p>
   147  
   148  <pre>
   149  $ cd go/src
   150  $ ./all.bash
   151  </pre>
   152  
   153  <p>
   154  (To build under Windows use <code>all.bat</code>.)
   155  </p>
   156  
   157  <p>
   158  If all goes well, it will finish by printing output like:
   159  </p>
   160  
   161  <pre>
   162  ALL TESTS PASSED
   163  
   164  ---
   165  Installed Go for linux/amd64 in /home/you/go.
   166  Installed commands in /home/you/go/bin.
   167  *** You need to add /home/you/go/bin to your $PATH. ***
   168  </pre>
   169  
   170  <p>
   171  where the details on the last few lines reflect the operating system,
   172  architecture, and root directory used during the install.
   173  </p>
   174  
   175  <div class="detail">
   176  <p>
   177  For more information about ways to control the build, see the discussion of
   178  <a href="#environment">environment variables</a> below.
   179  </p>
   180  </div>
   181  
   182  
   183  <h2 id="testing">Testing your installation</h2>
   184  
   185  <p>
   186  Check that Go is installed correctly by building a simple program.
   187  </p>
   188  
   189  <p>
   190  Create a file named <code>hello.go</code> and put the following program in it:
   191  </p>
   192  
   193  <pre>
   194  package main
   195  
   196  import "fmt"
   197  
   198  func main() {
   199      fmt.Printf("hello, world\n")
   200  }
   201  </pre>
   202  
   203  <p>
   204  Then run it with the <code>go</code> tool:
   205  </p>
   206  
   207  <pre>
   208  $ go run hello.go
   209  hello, world
   210  </pre>
   211  
   212  <p>
   213  If you see the "hello, world" message then Go is installed correctly.
   214  </p>
   215  
   216  <h2 id="gopath">Set up your work environment</h2>
   217  
   218  <p>
   219  The document <a href="/doc/code.html">How to Write Go Code</a> explains how to
   220  set up a work environment in which to build and test Go code.
   221  </p>
   222  
   223  <h2 id="community">Community resources</h2>
   224  
   225  <p>
   226  The usual community resources such as
   227  <code>#go-nuts</code> on the <a href="http://freenode.net/">Freenode</a> IRC server
   228  and the
   229  <a href="http://groups.google.com/group/golang-nuts">Go Nuts</a>
   230  mailing list have active developers that can help you with problems
   231  with your installation or your development work.
   232  For those who wish to keep up to date,
   233  there is another mailing list, <a href="http://groups.google.com/group/golang-checkins">golang-checkins</a>,
   234  that receives a message summarizing each checkin to the Go repository.
   235  </p>
   236  
   237  <p>
   238  Bugs can be reported using the <a href="http://code.google.com/p/go/issues/list">Go issue tracker</a>.
   239  </p>
   240  
   241  
   242  <h2 id="releases">Keeping up with releases</h2>
   243  
   244  <p>
   245  The Go project maintains a stable tag in its Mercurial repository:
   246  <code>release</code>.
   247  </p>
   248  
   249  <p>
   250  The <code>release</code> tag refers to the current stable release of Go.
   251  Most Go users should use this version. New releases are announced on the
   252  <a href="http://groups.google.com/group/golang-announce">golang-announce</a>
   253  mailing list.
   254  </p>
   255  
   256  <p>
   257  To update an existing tree to the latest release, you can run:
   258  </p>
   259  
   260  <pre>
   261  $ cd go/src
   262  $ hg pull
   263  $ hg update release
   264  $ ./all.bash
   265  </pre>
   266  
   267  
   268  <h2 id="environment">Optional environment variables</h2>
   269  
   270  <p>
   271  The Go compilation environment can be customized by environment variables.
   272  <i>None is required by the build</i>, but you may wish to set some
   273  to override the defaults.
   274  </p>
   275  
   276  <blockquote>
   277  
   278  <p><code>$GOROOT</code></p>
   279  <p>
   280  The root of the Go tree, often <code>$HOME/go</code>.
   281  Its value is built into the tree when it is compiled, and
   282  defaults to the parent of the directory where <code>all.bash</code> was run.
   283  There is no need to set this unless you want to switch between multiple
   284  local copies of the repository.
   285  </p>
   286  
   287  <p><code>$GOROOT_FINAL</code></p>
   288  <p>
   289  The value assumed by installed binaries and scripts when
   290  <code>$GOROOT</code> is not set explicitly.
   291  It defaults to the value of <code>$GOROOT</code>.
   292  If you want to build the Go tree in one location
   293  but move it elsewhere after the build, set 
   294  <code>$GOROOT_FINAL</code> to the eventual location.
   295  </p>
   296  
   297  <p><code>$GOOS</code> and <code>$GOARCH</code></p>
   298  <p>
   299  The name of the target operating system and compilation architecture.
   300  These default to the values of <code>$GOHOSTOS</code> and
   301  <code>$GOHOSTARCH</code> respectively (described below).
   302  
   303  <p>
   304  Choices for <code>$GOOS</code> are
   305  <code>darwin</code> (Mac OS X 10.6 and above), <code>freebsd</code>,
   306  <code>linux</code>, <code>netbsd</code>, <code>openbsd</code>, 
   307  <code>plan9</code>, and <code>windows</code>.
   308  Choices for <code>$GOARCH</code> are
   309  <code>amd64</code> (64-bit x86, the most mature port),
   310  <code>386</code> (32-bit x86), and <code>arm</code> (32-bit ARM).
   311  The valid combinations of <code>$GOOS</code> and <code>$GOARCH</code> are:
   312  <table cellpadding="0">
   313  <tr>
   314  <th width="50"><th align="left" width="100"><code>$GOOS</code></th> <th align="left" width="100"><code>$GOARCH</code></th> <th align="left"></th>
   315  </tr>
   316  <tr>
   317  <td></td><td><code>darwin</code></td> <td><code>386</code></td>
   318  </tr>
   319  <tr>
   320  <td></td><td><code>darwin</code></td> <td><code>amd64</code></td>
   321  </tr>
   322  <tr>
   323  <td></td><td><code>freebsd</code></td> <td><code>386</code></td>
   324  </tr>
   325  <tr>
   326  <td></td><td><code>freebsd</code></td> <td><code>amd64</code></td>
   327  </tr>
   328  <tr>
   329  <td></td><td><code>linux</code></td> <td><code>386</code></td>
   330  </tr>
   331  <tr>
   332  <td></td><td><code>linux</code></td> <td><code>amd64</code></td>
   333  </tr>
   334  <tr>
   335  <td></td><td><code>linux</code></td> <td><code>arm</code></td>
   336  </tr>
   337  <tr>
   338  <td></td><td><code>netbsd</code></td> <td><code>386</code></td>
   339  </tr>
   340  <tr>
   341  <td></td><td><code>netbsd</code></td> <td><code>amd64</code></td>
   342  </tr>
   343  <tr>
   344  <td></td><td><code>openbsd</code></td> <td><code>386</code></td>
   345  </tr>
   346  <tr>
   347  <td></td><td><code>openbsd</code></td> <td><code>amd64</code></td>
   348  </tr>
   349  <tr>
   350  <td></td><td><code>plan9</code></td> <td><code>386</code></td>
   351  </tr>
   352  <tr>
   353  <td></td><td><code>windows</code></td> <td><code>386</code></td>
   354  </tr>
   355  <tr>
   356  <td></td><td><code>windows</code></td> <td><code>amd64</code></td>
   357  </tr>
   358  </table>
   359  
   360  <p><code>$GOHOSTOS</code> and <code>$GOHOSTARCH</code></p>
   361  <p>
   362  The name of the host operating system and compilation architecture.
   363  These default to the local system's operating system and
   364  architecture.
   365  </p>
   366  
   367  <p>
   368  Valid choices are the same as for <code>$GOOS</code> and
   369  <code>$GOARCH</code>, listed above.
   370  The specified values must be compatible with the local system.
   371  For example, you should not set <code>$GOHOSTARCH</code> to 
   372  <code>arm</code> on an x86 system.
   373  </p>
   374  
   375  <p><code>$GOBIN</code>
   376  <p>
   377  The location where Go binaries will be installed.
   378  The default is <code>$GOROOT/bin</code>.
   379  After installing, you will want to arrange to add this
   380  directory to your <code>$PATH</code>, so you can use the tools.
   381  If <code>$GOBIN</code> is set, the <a href="/cmd/go">go command</a>
   382  installs all commands there.
   383  </p>
   384  
   385  <p><code>$GOARM</code> (arm, default=6)</p>
   386  <p>
   387  The ARM architecture version the run-time libraries should target.
   388  Setting <code>$GOARM</code> to 5 causes the linker to emit calls
   389  to a software floating point implementation instead of using
   390  hardware floating point support.
   391  </p>
   392  
   393  </blockquote>
   394  
   395  <p>
   396  Note that <code>$GOARCH</code> and <code>$GOOS</code> identify the
   397  <em>target</em> environment, not the environment you are running on.
   398  In effect, you are always cross-compiling.
   399  By architecture, we mean the kind of binaries
   400  that the target environment can run:
   401  an x86-64 system running a 32-bit-only operating system
   402  must set <code>GOARCH</code> to <code>386</code>,
   403  not <code>amd64</code>.
   404  </p>
   405  
   406  <p>
   407  If you choose to override the defaults,
   408  set these variables in your shell profile (<code>$HOME/.bashrc</code>,
   409  <code>$HOME/.profile</code>, or equivalent). The settings might look 
   410  something like this:
   411  </p>
   412  
   413  <pre>
   414  export GOROOT=$HOME/go
   415  export GOARCH=amd64
   416  export GOOS=linux
   417  </pre>
   418  
   419  <p>
   420  although, to reiterate, none of these variables needs to be set to build,
   421  install, and develop the Go tree.
   422  </p>