github.com/tcnksm/go@v0.0.0-20141208075154-439b32936367/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 Linux, FreeBSD and NetBSD binaries. Less widely used than the other ports.
    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 DragonFly BSD, FreeBSD, Linux, NetBSD, OpenBSD,
    73  OS X (Darwin), Plan 9, Solaris 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="//golang.org/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  <code>all.bash</code> (or <code>all.bat</code>) runs important tests for Go,
   180  which can take more time than simply building Go. If you do not want to run
   181  the test suite use <code>make.bash</code> (or <code>make.bat</code>)
   182  instead.
   183  </p>
   184  </div>
   185  
   186  
   187  <h2 id="testing">Testing your installation</h2>
   188  
   189  <p>
   190  Check that Go is installed correctly by building a simple program.
   191  </p>
   192  
   193  <p>
   194  Create a file named <code>hello.go</code> and put the following program in it:
   195  </p>
   196  
   197  <pre>
   198  package main
   199  
   200  import "fmt"
   201  
   202  func main() {
   203      fmt.Printf("hello, world\n")
   204  }
   205  </pre>
   206  
   207  <p>
   208  Then run it with the <code>go</code> tool:
   209  </p>
   210  
   211  <pre>
   212  $ go run hello.go
   213  hello, world
   214  </pre>
   215  
   216  <p>
   217  If you see the "hello, world" message then Go is installed correctly.
   218  </p>
   219  
   220  <h2 id="gopath">Set up your work environment</h2>
   221  
   222  <p>
   223  You're almost done.
   224  You just need to do a little more setup.
   225  </p>
   226  
   227  <p>
   228  <a href="/doc/code.html" class="download" id="start">
   229  <span class="big">How to Write Go Code</span>
   230  <span class="desc">Learn how to set up and use the Go tools</span>
   231  </a>
   232  </p>
   233  
   234  <p>
   235  The <a href="/doc/code.html">How to Write Go Code</a> document 
   236  provides <b>essential setup instructions</b> for using the Go tools.
   237  </p>
   238  
   239  
   240  <h2 id="tools">Install additional tools</h2>
   241  
   242  <p>
   243  The source code for several Go tools (including <a href="/cmd/godoc/">godoc</a>)
   244  is kept in <a href="https://golang.org/x/tools">the go.tools repository</a>.
   245  To install all of them, run the <code>go</code> <code>get</code> command:
   246  </p>
   247  
   248  <pre>
   249  $ go get golang.org/x/tools/cmd/...
   250  </pre>
   251  
   252  <p>
   253  Or if you just want to install a specific command (<code>godoc</code> in this case):
   254  </p>
   255  
   256  <pre>
   257  $ go get golang.org/x/tools/cmd/godoc
   258  </pre>
   259  
   260  <p>
   261  To install these tools, the <code>go</code> <code>get</code> command requires 
   262  that <a href="#mercurial">Mercurial</a> be installed locally.
   263  </p>
   264  
   265  <p>
   266  You must also have a workspace (<code>GOPATH</code>) set up;
   267  see <a href="/doc/code.html">How to Write Go Code</a> for the details.
   268  </p>
   269  
   270  <p>
   271  <b>Note</b>: The <code>go</code> command will install the <code>godoc</code>
   272  binary to <code>$GOROOT/bin</code> (or <code>$GOBIN</code>) and the
   273  <code>cover</code> and <code>vet</code> binaries to
   274  <code>$GOROOT/pkg/tool/$GOOS_$GOARCH</code>.
   275  You can access the latter commands with
   276  "<code>go</code> <code>tool</code> <code>cover</code>" and
   277  "<code>go</code> <code>tool</code> <code>vet</code>".
   278  </p>
   279  
   280  <h2 id="community">Community resources</h2>
   281  
   282  <p>
   283  The usual community resources such as
   284  <code>#go-nuts</code> on the <a href="http://freenode.net/">Freenode</a> IRC server
   285  and the
   286  <a href="//groups.google.com/group/golang-nuts">Go Nuts</a>
   287  mailing list have active developers that can help you with problems
   288  with your installation or your development work.
   289  For those who wish to keep up to date,
   290  there is another mailing list, <a href="//groups.google.com/group/golang-checkins">golang-checkins</a>,
   291  that receives a message summarizing each checkin to the Go repository.
   292  </p>
   293  
   294  <p>
   295  Bugs can be reported using the <a href="//code.google.com/p/go/issues/list">Go issue tracker</a>.
   296  </p>
   297  
   298  
   299  <h2 id="releases">Keeping up with releases</h2>
   300  
   301  <p>
   302  The Go project maintains a stable tag in its Mercurial repository:
   303  <code>release</code>.
   304  </p>
   305  
   306  <p>
   307  The <code>release</code> tag refers to the current stable release of Go.
   308  Most Go users should use this version. New releases are announced on the
   309  <a href="//groups.google.com/group/golang-announce">golang-announce</a>
   310  mailing list.
   311  </p>
   312  
   313  <p>
   314  To update an existing tree to the latest release, you can run:
   315  </p>
   316  
   317  <pre>
   318  $ cd go/src
   319  $ hg pull
   320  $ hg update release
   321  $ ./all.bash
   322  </pre>
   323  
   324  
   325  <h2 id="environment">Optional environment variables</h2>
   326  
   327  <p>
   328  The Go compilation environment can be customized by environment variables.
   329  <i>None is required by the build</i>, but you may wish to set some
   330  to override the defaults.
   331  </p>
   332  
   333  <ul>
   334  <li><code>$GOROOT</code>
   335  <p>
   336  The root of the Go tree, often <code>$HOME/go</code>.
   337  Its value is built into the tree when it is compiled, and
   338  defaults to the parent of the directory where <code>all.bash</code> was run.
   339  There is no need to set this unless you want to switch between multiple
   340  local copies of the repository.
   341  </p>
   342  
   343  <li><code>$GOROOT_FINAL</code>
   344  <p>
   345  The value assumed by installed binaries and scripts when
   346  <code>$GOROOT</code> is not set explicitly.
   347  It defaults to the value of <code>$GOROOT</code>.
   348  If you want to build the Go tree in one location
   349  but move it elsewhere after the build, set 
   350  <code>$GOROOT_FINAL</code> to the eventual location.
   351  </p>
   352  
   353  <li><code>$GOOS</code> and <code>$GOARCH</code>
   354  <p>
   355  The name of the target operating system and compilation architecture.
   356  These default to the values of <code>$GOHOSTOS</code> and
   357  <code>$GOHOSTARCH</code> respectively (described below).
   358  
   359  <p>
   360  Choices for <code>$GOOS</code> are
   361  <code>darwin</code> (Mac OS X 10.6 and above), <code>dragonfly</code>, <code>freebsd</code>,
   362  <code>linux</code>, <code>netbsd</code>, <code>openbsd</code>, 
   363  <code>plan9</code>, <code>solaris</code> and <code>windows</code>.
   364  Choices for <code>$GOARCH</code> are
   365  <code>amd64</code> (64-bit x86, the most mature port),
   366  <code>386</code> (32-bit x86), and <code>arm</code> (32-bit ARM).
   367  The valid combinations of <code>$GOOS</code> and <code>$GOARCH</code> are:
   368  <table cellpadding="0">
   369  <tr>
   370  <th width="50"></th><th align="left" width="100"><code>$GOOS</code></th> <th align="left" width="100"><code>$GOARCH</code></th>
   371  </tr>
   372  <tr>
   373  <td></td><td><code>darwin</code></td> <td><code>386</code></td>
   374  </tr>
   375  <tr>
   376  <td></td><td><code>darwin</code></td> <td><code>amd64</code></td>
   377  </tr>
   378  <tr>
   379  <td></td><td><code>dragonfly</code></td> <td><code>386</code></td>
   380  </tr>
   381  <tr>
   382  <td></td><td><code>dragonfly</code></td> <td><code>amd64</code></td>
   383  </tr>
   384  <tr>
   385  <td></td><td><code>freebsd</code></td> <td><code>386</code></td>
   386  </tr>
   387  <tr>
   388  <td></td><td><code>freebsd</code></td> <td><code>amd64</code></td>
   389  </tr>
   390  <tr>
   391  <td></td><td><code>freebsd</code></td> <td><code>arm</code></td>
   392  </tr>
   393  <tr>
   394  <td></td><td><code>linux</code></td> <td><code>386</code></td>
   395  </tr>
   396  <tr>
   397  <td></td><td><code>linux</code></td> <td><code>amd64</code></td>
   398  </tr>
   399  <tr>
   400  <td></td><td><code>linux</code></td> <td><code>arm</code></td>
   401  </tr>
   402  <tr>
   403  <td></td><td><code>netbsd</code></td> <td><code>386</code></td>
   404  </tr>
   405  <tr>
   406  <td></td><td><code>netbsd</code></td> <td><code>amd64</code></td>
   407  </tr>
   408  <tr>
   409  <td></td><td><code>netbsd</code></td> <td><code>arm</code></td>
   410  </tr>
   411  <tr>
   412  <td></td><td><code>openbsd</code></td> <td><code>386</code></td>
   413  </tr>
   414  <tr>
   415  <td></td><td><code>openbsd</code></td> <td><code>amd64</code></td>
   416  </tr>
   417  <tr>
   418  <td></td><td><code>plan9</code></td> <td><code>386</code></td>
   419  </tr>
   420  <tr>
   421  <td></td><td><code>plan9</code></td> <td><code>amd64</code></td>
   422  </tr>
   423  <tr>
   424  <td></td><td><code>solaris</code></td> <td><code>amd64</code></td>
   425  </tr>
   426  <tr>
   427  <td></td><td><code>windows</code></td> <td><code>386</code></td>
   428  </tr>
   429  <tr>
   430  <td></td><td><code>windows</code></td> <td><code>amd64</code></td>
   431  </tr>
   432  </table>
   433  
   434  <li><code>$GOHOSTOS</code> and <code>$GOHOSTARCH</code>
   435  <p>
   436  The name of the host operating system and compilation architecture.
   437  These default to the local system's operating system and
   438  architecture.
   439  </p>
   440  
   441  <p>
   442  Valid choices are the same as for <code>$GOOS</code> and
   443  <code>$GOARCH</code>, listed above.
   444  The specified values must be compatible with the local system.
   445  For example, you should not set <code>$GOHOSTARCH</code> to 
   446  <code>arm</code> on an x86 system.
   447  </p>
   448  
   449  <li><code>$GOBIN</code>
   450  <p>
   451  The location where Go binaries will be installed.
   452  The default is <code>$GOROOT/bin</code>.
   453  After installing, you will want to arrange to add this
   454  directory to your <code>$PATH</code>, so you can use the tools.
   455  If <code>$GOBIN</code> is set, the <a href="/cmd/go">go command</a>
   456  installs all commands there.
   457  </p>
   458  
   459  <li><code>$GO386</code> (for <code>386</code> only, default is auto-detected
   460  if built on either <code>386</code> or <code>amd64</code>, <code>387</code> otherwise)
   461  <p>
   462  This controls the code generated by 8g to use either the 387 floating-point unit
   463  (set to <code>387</code>) or SSE2 instructions (set to <code>sse2</code>) for
   464  floating point computations.
   465  </p>
   466  <ul>
   467  	<li><code>GO386=387</code>: use x87 for floating point operations; should support all x86 chips (Pentium MMX or later).
   468  	<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.
   469  </ul>
   470  
   471  <li><code>$GOARM</code> (for <code>arm</code> only; default is auto-detected if building
   472  on the target processor, 6 if not)
   473  <p>
   474  This sets the ARM floating point co-processor architecture version the run-time
   475  should target. If you are compiling on the target system, its value will be auto-detected.
   476  </p>
   477  <ul>
   478  	<li><code>GOARM=5</code>: use software floating point; when CPU doesn't have VFP co-processor
   479  	<li><code>GOARM=6</code>: use VFPv1 only; default if cross compiling; usually ARM11 or better cores (VFPv2 or better is also supported)
   480  	<li><code>GOARM=7</code>: use VFPv3; usually Cortex-A cores
   481  </ul>
   482  <p>
   483  If in doubt, leave this variable unset, and adjust it if required
   484  when you first run the Go executable.
   485  The <a href="//golang.org/wiki/GoArm">GoARM</a> page
   486  on the <a href="//golang.org/wiki">Go community wiki</a>
   487  contains further details regarding Go's ARM support.
   488  </p>
   489  
   490  </ul>
   491  
   492  <p>
   493  Note that <code>$GOARCH</code> and <code>$GOOS</code> identify the
   494  <em>target</em> environment, not the environment you are running on.
   495  In effect, you are always cross-compiling.
   496  By architecture, we mean the kind of binaries
   497  that the target environment can run:
   498  an x86-64 system running a 32-bit-only operating system
   499  must set <code>GOARCH</code> to <code>386</code>,
   500  not <code>amd64</code>.
   501  </p>
   502  
   503  <p>
   504  If you choose to override the defaults,
   505  set these variables in your shell profile (<code>$HOME/.bashrc</code>,
   506  <code>$HOME/.profile</code>, or equivalent). The settings might look 
   507  something like this:
   508  </p>
   509  
   510  <pre>
   511  export GOROOT=$HOME/go
   512  export GOARCH=amd64
   513  export GOOS=linux
   514  </pre>
   515  
   516  <p>
   517  although, to reiterate, none of these variables needs to be set to build,
   518  install, and develop the Go tree.
   519  </p>