blob: 2256123ecb8daf30fbb5e48b1de53f41fad4af89 [file] [log] [blame]
Andrew Gerrand6ab8d242010-09-16 14:01:02 +10001<!-- Getting Started -->
Russ Coxfb39a4d2009-10-23 15:24:08 -07002
Russ Cox86920ad2010-08-24 20:00:50 -04003<h2 id="introduction">Introduction</h2>
Russ Coxfb39a4d2009-10-23 15:24:08 -07004
Russ Cox6fff7b22009-11-08 22:12:51 -08005<p>Go is an open source project, distributed under a
6<a href="/LICENSE">BSD-style license</a>.
7This document explains how to check out the sources,
8build them on your own machine, and run them.
9</p>
10
Andrew Gerrand6ab8d242010-09-16 14:01:02 +100011<div class="detail">
12
Russ Coxfb39a4d2009-10-23 15:24:08 -070013<p>
14There are two distinct ways to experiment with Go.
Russ Cox6fff7b22009-11-08 22:12:51 -080015This document focuses on the <code>gc</code> Go
Rob Pike5f50a812009-11-05 15:07:42 -080016compiler and tools (<code>6g</code>, <code>8g</code> etc.).
Russ Coxfb39a4d2009-10-23 15:24:08 -070017For information on how to use <code>gccgo</code>, a more traditional
Rob Pike5f50a812009-11-05 15:07:42 -080018compiler using the GCC back end, see
Ian Lance Taylor5b387fb2009-11-06 14:36:34 -080019<a href="gccgo_install.html">Setting up and using gccgo</a>.
Russ Coxfb39a4d2009-10-23 15:24:08 -070020</p>
21
Rob Pike5f50a812009-11-05 15:07:42 -080022<p>
Russ Coxa24f8142010-03-16 18:45:16 -070023The Go compilers support three instruction sets.
Rob Pike5f50a812009-11-05 15:07:42 -080024There are important differences in the quality of the compilers for the different
25architectures.
26</p>
27
28<dl>
29<dt>
30 <code>amd64</code> (a.k.a. <code>x86-64</code>); <code>6g,6l,6c,6a</code>
31</dt>
32<dd>
Rob Pikeb2d37012009-11-06 17:31:23 -080033 The most mature implementation. The compiler has an effective optimizer
Rob Pike5f50a812009-11-05 15:07:42 -080034 (registerizer) and generates good code (although <code>gccgo</code>
35 can do noticeably better sometimes).
36</dd>
37<dt>
38 <code>386</code> (a.k.a. <code>x86</code> or <code>x86-32</code>); <code>8g,8l,8c,8a</code>
39</dt>
Russ Cox6fff7b22009-11-08 22:12:51 -080040<dd>
Russ Cox86920ad2010-08-24 20:00:50 -040041 Comparable to the <code>amd64</code> port.
Rob Pike5f50a812009-11-05 15:07:42 -080042</dd>
43<dt>
44 <code>arm</code> (a.k.a. <code>ARM</code>); <code>5g,5l,5c,5a</code>
45</dt>
46<dd>
Russ Coxb7cb8442010-09-22 15:30:42 +100047 Incomplete.
Rob Pike80e25fc2011-01-19 23:07:38 -050048 It only supports Linux binaries, the optimizer is incomplete,
49 and floating point uses the VFP unit.
Russ Coxe8bde0e2010-10-26 08:34:40 -070050 However, all tests pass.
Rob Pike80e25fc2011-01-19 23:07:38 -050051 Work on the optimizer is continuing.
Russ Coxb7cb8442010-09-22 15:30:42 +100052 Tested against a Nexus One.
Rob Pike5f50a812009-11-05 15:07:42 -080053</dd>
54</dl>
55
56<p>
Rob Pike966bf712011-03-01 13:54:22 -080057Except for things like low-level operating system interface code, the run-time
Rob Pike5f50a812009-11-05 15:07:42 -080058support is the same in all ports and includes a mark-and-sweep garbage collector
59(a fancier one is in the works), efficient array and string slicing,
60support for segmented stacks, and a strong goroutine implementation.
61</p>
62
63<p>
Russ Cox0c542252010-12-15 11:49:23 -050064The compilers can target the FreeBSD, Linux,
Russ Cox86920ad2010-08-24 20:00:50 -040065and OS X (a.k.a. Darwin) operating systems.
Andrew Gerrandab7884d2010-12-08 08:31:26 +110066(A port to Microsoft Windows is in progress but incomplete. See the
67<a href="http://code.google.com/p/go/wiki/WindowsPort">Windows Port</a>
68page for details.)
Russ Cox86920ad2010-08-24 20:00:50 -040069The full set of supported combinations is listed in the discussion of
70<a href="#environment">environment variables</a> below.
Rob Pike5f50a812009-11-05 15:07:42 -080071</p>
72
Andrew Gerrand6ab8d242010-09-16 14:01:02 +100073</div>
74
Russ Cox86920ad2010-08-24 20:00:50 -040075<h2 id="ctools">Install C tools, if needed</h2>
Robert Griesemer7cd4de62010-03-10 16:31:09 -080076
Andrew Gerrand6ab8d242010-09-16 14:01:02 +100077<p>The Go tool chain is written in C.
78To build it, you need these programs installed:
79<ul>
80<li>GCC,
81<li>the standard C libraries,
82<li>the parser generator Bison,
Russ Coxc3e6e6e2011-04-26 21:39:22 -040083<li>GNU <tt>make</tt> (version 3.81 or later),
Andrew Gerrand6ab8d242010-09-16 14:01:02 +100084<li><tt>awk</tt>, and
85<li>the text editor <tt>ed</tt>.
86</ul>
Robert Griesemer7cd4de62010-03-10 16:31:09 -080087</p>
88
Russ Cox86920ad2010-08-24 20:00:50 -040089<p>On OS X, they can be
90installed as part of
91<a href="http://developer.apple.com/TOOLS/Xcode/">Xcode</a>.
92</p>
Robert Griesemer7cd4de62010-03-10 16:31:09 -080093
Andrew Gerrandeb5e4b82011-04-14 10:28:59 +100094<p>On Ubuntu/Debian, use <code>sudo apt-get install bison ed gawk gcc libc6-dev
95make</code>. If you want to build 32-bit binaries on a 64-bit system you'll
96also need the <code>libc6-dev-i386</code> package.
Andrew Gerrand6ab8d242010-09-16 14:01:02 +100097</p>
98
99<h2 id="mercurial">Install Mercurial, if needed</h2>
100
101<p>
Robert Griesemer686490c2010-09-29 12:34:24 -0700102To perform the next step you must have Mercurial installed. (Check that you have an <code>hg</code> command.) This suffices to install Mercurial on most systems:
Andrew Gerrand6ab8d242010-09-16 14:01:02 +1000103</p>
104<pre>
105sudo easy_install mercurial
106</pre>
107(On Ubuntu/Debian, you might try <code>apt-get install python-setuptools
108python-dev build-essential</code> first. The Mercurial in your distribution's
109package repository will most likely be old and broken.)
110</p>
111<p>
112If that fails, try installing manually from the <a href="http://mercurial.selenic.com/wiki/Download">Mercurial Download</a> page.</p>
Robert Griesemer7cd4de62010-03-10 16:31:09 -0800113</p>
114
Robert Griesemer68b40fb2011-02-02 15:53:32 -0800115<p>
116Mercurial versions 1.7.x and up require the configuration of
117<a href="http://mercurial.selenic.com/wiki/CACertificates">Certification Authorities</a>
118(CAs). Error messages of the form:
119</p>
120<pre>
121warning: go.googlecode.com certificate with fingerprint b1:af: ... bc not verified (check hostfingerprints or web.cacerts config setting)
122</pre>
123<p>
124when using Mercurial indicate that the CAs are missing.
125Check your Mercurial version (<code>hg --version</code>) and
126<a href="http://mercurial.selenic.com/wiki/CACertificates#Configuration_of_HTTPS_certificate_authorities">configure the CAs</a>
127if necessary.
128</p>
129
Russ Cox86920ad2010-08-24 20:00:50 -0400130<h2 id="fetch">Fetch the repository</h2>
Russ Coxfb39a4d2009-10-23 15:24:08 -0700131
132<p>
Russ Cox86920ad2010-08-24 20:00:50 -0400133<p>Go will install to a directory named <code>go</code>.
134Change to the directory that will be its parent
135and make sure the <code>go</code> directory does not exist.
Russ Coxfb39a4d2009-10-23 15:24:08 -0700136Then check out the repository:</p>
137
Russ Coxfb39a4d2009-10-23 15:24:08 -0700138<pre>
Andrew Gerranda3baf7f2011-05-02 20:40:47 -0700139$ hg clone -u release https://go.googlecode.com/hg/ go
Russ Coxfb39a4d2009-10-23 15:24:08 -0700140</pre>
141
Russ Cox86920ad2010-08-24 20:00:50 -0400142<h2 id="install">Install Go</h2>
Russ Coxfb39a4d2009-10-23 15:24:08 -0700143
Russ Coxfb39a4d2009-10-23 15:24:08 -0700144<p>
Devon H. O'Dell857d4cf2009-12-11 15:14:09 -0800145To build the Go distribution, run
Russ Coxfb39a4d2009-10-23 15:24:08 -0700146</p>
147
148<pre>
Russ Cox86920ad2010-08-24 20:00:50 -0400149$ cd go/src
Christopher Wedgwood34191d92010-01-25 00:09:46 -0800150$ ./all.bash
Russ Coxfb39a4d2009-10-23 15:24:08 -0700151</pre>
152
153<p>
Russ Coxda392d92010-08-18 10:08:49 -0400154If all goes well, it will finish by printing output like:
Russ Coxfb39a4d2009-10-23 15:24:08 -0700155</p>
156
157<pre>
Russ Coxb9f94762011-02-14 09:27:02 -0500158ALL TESTS PASSED
Russ Coxda392d92010-08-18 10:08:49 -0400159
160---
Russ Cox86920ad2010-08-24 20:00:50 -0400161Installed Go for linux/amd64 in /home/you/go.
162Installed commands in /home/you/go/bin.
163*** You need to add /home/you/go/bin to your $PATH. ***
164The compiler is 6g.
Russ Coxfb39a4d2009-10-23 15:24:08 -0700165</pre>
166
167<p>
Andrew Gerrand3fea5ba2011-03-03 16:41:03 +1100168where the details on the last few lines reflect the operating system,
Russ Coxda392d92010-08-18 10:08:49 -0400169architecture, and root directory used during the install.
Russ Coxfb39a4d2009-10-23 15:24:08 -0700170</p>
171
Andrew Gerrand6ab8d242010-09-16 14:01:02 +1000172<div class="detail">
173
Russ Cox86920ad2010-08-24 20:00:50 -0400174<p>For more information about ways to control the build,
175see the discussion of <a href="#environment">environment variables</a> below.</p>
Andrew Gerrand6ab8d242010-09-16 14:01:02 +1000176</div>
Russ Cox86920ad2010-08-24 20:00:50 -0400177
178<h2 id="writing">Writing programs</h2>
Russ Coxfb39a4d2009-10-23 15:24:08 -0700179
180<p>
181Given a file <code>file.go</code>, compile it using
182</p>
183
184<pre>
185$ 6g file.go
186</pre>
187
188<p>
189<code>6g</code> is the Go compiler for <code>amd64</code>; it will write the output
190in <code>file.6</code>. The &lsquo;<code>6</code>&rsquo; identifies
191files for the <code>amd64</code> architecture.
192The identifier letters for <code>386</code> and <code>arm</code>
193are &lsquo;<code>8</code>&rsquo; and &lsquo;<code>5</code>&rsquo;.
194That is, if you were compiling for <code>386</code>, you would use
195<code>8g</code> and the output would be named <code>file.8</code>.
196</p>
197
198<p>
199To link the file, use
200</p>
201
202<pre>
203$ 6l file.6
204</pre>
205
206<p>
207and to run it
208</p>
209
210<pre>
211$ ./6.out
212</pre>
213
214<p>A complete example:
215</p>
216
217<pre>
218$ cat &gt;hello.go &lt;&lt;EOF
219package main
220
221import "fmt"
222
223func main() {
224 fmt.Printf("hello, world\n")
225}
226EOF
227$ 6g hello.go
228$ 6l hello.6
229$ ./6.out
230hello, world
231$
232</pre>
233
234<p>
235There is no need to list <code>hello.6</code>'s package dependencies
236(in this case, package <code>fmt</code>) on the <code>6l</code>
237command line.
238The linker learns about them by reading <code>hello.6</code>.
239</p>
240
Andrew Gerrand6ab8d242010-09-16 14:01:02 +1000241<div class="detail">
Russ Coxfb39a4d2009-10-23 15:24:08 -0700242<p>
243To build more complicated programs, you will probably
Russ Cox6fff7b22009-11-08 22:12:51 -0800244want to use a
Russ Coxfb39a4d2009-10-23 15:24:08 -0700245<code>Makefile</code>.
Rob Pike5f50a812009-11-05 15:07:42 -0800246There are examples in places like
Russ Cox86920ad2010-08-24 20:00:50 -0400247<code>go/src/cmd/godoc/Makefile</code>
248and <code>go/src/pkg/*/Makefile</code>.
Rob Pike5f50a812009-11-05 15:07:42 -0800249The
250<a href="contribute.html">document</a>
251about contributing to the Go project
252gives more detail about
Russ Coxfb39a4d2009-10-23 15:24:08 -0700253the process of building and testing Go programs.
254</p>
Andrew Gerrand6ab8d242010-09-16 14:01:02 +1000255</div>
256
257<h2 id="next">What's next</h2>
258
259<p>
260Start by reading the <a href="go_tutorial.html">Go Tutorial</a>.
261</p>
262
263<p>
264Build a web application by following the <a href="codelab/wiki/">Wiki
265Codelab</a>.
266</p>
267
268<p>
269Read <a href="effective_go.html">Effective Go</a> to learn about writing
270idiomatic Go code.
271</p>
272
273<p>
274For the full story, consult Go's extensive
275<a href="docs.html">documentation</a>.
276</p>
Russ Cox8b04cef2009-11-08 23:38:30 -0800277
Russ Cox86920ad2010-08-24 20:00:50 -0400278<h2 id="releases">Keeping up with releases</h2>
Russ Cox32696472009-12-15 19:16:56 -0800279
Andrew Gerrand929449d2011-03-17 16:33:10 +1100280<p>
281The Go project maintains two stable tags in its Mercurial repository:
282<code>release</code> and <code>weekly</code>.
283The <code>weekly</code> tag is updated about once a week, and should be used by
284those who want to track the project's development.
285The <code>release</code> tag is given, less often, to those weekly releases
286that have proven themselves to be robust.
287</p>
288
289<p>
290Most Go users will want to keep their Go installation at the latest
291<code>release</code> tag.
292New releases are announced on the
293<a href="http://groups.google.com/group/golang-announce">golang-announce</a>
294mailing list.
295</p>
296
297<p>
Russ Cox32696472009-12-15 19:16:56 -0800298To update an existing tree to the latest release, you can run:
299</p>
300
301<pre>
Russ Cox86920ad2010-08-24 20:00:50 -0400302$ cd go/src
Russ Cox32696472009-12-15 19:16:56 -0800303$ hg pull
304$ hg update release
Russ Cox05f26362010-01-28 18:18:40 -0800305$ ./all.bash
Russ Cox32696472009-12-15 19:16:56 -0800306</pre>
307
Andrew Gerrand929449d2011-03-17 16:33:10 +1100308<p>
309To use the <code>weekly</code> tag run <code>hg update weekly</code> instead.
310</p>
311
Russ Cox86920ad2010-08-24 20:00:50 -0400312<h2 id="community">Community resources</h2>
Russ Cox8b04cef2009-11-08 23:38:30 -0800313
314<p>
315For real-time help, there may be users or developers on
316<code>#go-nuts</code> on the <a href="http://freenode.net/">Freenode</a> IRC server.
317</p>
318
319<p>
320The official mailing list for discussion of the Go language is
321<a href="http://groups.google.com/group/golang-nuts">Go Nuts</a>.
322</p>
323
324<p>
325Bugs can be reported using the <a href="http://code.google.com/p/go/issues/list">Go issue tracker</a>.
326</p>
327
328<p>
329For those who wish to keep up with development,
330there is another mailing list, <a href="http://groups.google.com/group/golang-checkins">golang-checkins</a>,
331that receives a message summarizing each checkin to the Go repository.
332</p>
333
Russ Cox86920ad2010-08-24 20:00:50 -0400334<h2 id="environment">Environment variables</h2>
Russ Cox8b04cef2009-11-08 23:38:30 -0800335
Russ Cox86920ad2010-08-24 20:00:50 -0400336<p>
Andrew Gerrandf89050d2010-12-05 12:04:15 +0900337The Go compilation environment can be customized by environment variables.
Russ Cox86920ad2010-08-24 20:00:50 -0400338None are required by the build, but you may wish to set them
339to override the defaults.
340</p>
341
342<dl>
343<dt>
344 <code>$GOROOT</code>
345</dt>
346<dd>
347 The root of the Go tree, often <code>$HOME/go</code>.
348 This defaults to the parent of the directory where <code>all.bash</code> is run.
349 If you choose not to set <code>$GOROOT</code>, you must
350 run <code>gomake</code> instead of <code>make</code> or <code>gmake</code>
351 when developing Go programs using the conventional makefiles.
352</dd>
353
354<dt>
355 <code>$GOROOT_FINAL</code>
356</dt>
357<dd>
358 The value assumed by installed binaries and scripts when
359 <code>$GOROOT</code> is not set.
360 It defaults to the value used for <code>$GOROOT</code>.
361 If you want to build the Go tree in one location
362 but move it elsewhere after the build, set
363 <code>$GOROOT_FINAL</code> to the eventual location.
364</dd>
365
366<dt>
367<code>$GOOS</code> and <code>$GOARCH</code>
368</dt>
369<dd>
370 The name of the target operating system and compilation architecture.
Andrew Gerrandf89050d2010-12-05 12:04:15 +0900371 These default to the values of <code>$GOHOSTOS</code> and
372 <code>$GOHOSTARCH</code> respectively (described below).
Russ Cox86920ad2010-08-24 20:00:50 -0400373
374 <p>
375 Choices for <code>$GOOS</code> are <code>linux</code>,
376 <code>freebsd</code>,
377 <code>darwin</code> (Mac OS X 10.5 or 10.6),
Peter Mundyc17394df2010-12-08 20:27:45 +1100378 and <code>windows</code> (Windows, an incomplete port).
Russ Cox86920ad2010-08-24 20:00:50 -0400379 Choices for <code>$GOARCH</code> are <code>amd64</code> (64-bit x86, the most mature port),
380 <code>386</code> (32-bit x86), and
381 <code>arm</code> (32-bit ARM, an incomplete port).
382 The valid combinations of <code>$GOOS</code> and <code>$GOARCH</code> are:
Russ Cox86920ad2010-08-24 20:00:50 -0400383 <table cellpadding="0">
384 <tr>
385 <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>
386 </tr>
387 <tr>
388 <td></td><td><code>darwin</code></td> <td><code>386</code></td>
389 </tr>
390 <tr>
391 <td></td><td><code>darwin</code></td> <td><code>amd64</code></td>
392 </tr>
393 <tr>
394 <td></td><td><code>freebsd</code></td> <td><code>386</code></td>
395 </tr>
396 <tr>
397 <td></td><td><code>freebsd</code></td> <td><code>amd64</code></td>
398 </tr>
399 <tr>
400 <td></td><td><code>linux</code></td> <td><code>386</code></td>
401 </tr>
402 <tr>
403 <td></td><td><code>linux</code></td> <td><code>amd64</code></td>
404 </tr>
405 <tr>
406 <td></td><td><code>linux</code></td> <td><code>arm</code></td> <td><i>incomplete</i></td>
407 </tr>
408 <tr>
Russ Cox86920ad2010-08-24 20:00:50 -0400409 <td></td><td><code>windows</code></td> <td><code>386</code></td> <td><i>incomplete</i></td>
410 </tr>
411 </table>
412</dd>
413
414<dt>
Andrew Gerrandf89050d2010-12-05 12:04:15 +0900415<code>$GOHOSTOS</code> and <code>$GOHOSTARCH</code>
416</dt>
417<dd>
418 The name of the host operating system and compilation architecture.
419 These default to the local system's operating system and
420 architecture.
421
422 <p>
423 Valid choices are the same as for <code>$GOOS</code> and
424 <code>$GOARCH</code>, listed above.
425 The specified values must be compatible with the local system.
426 For example, you should not set <code>$GOHOSTARCH</code> to
427 <code>arm</code> on an x86 system.
428</dd>
429
430<dt>
Russ Cox86920ad2010-08-24 20:00:50 -0400431<code>$GOBIN</code>
432</dt>
433<dd>
434 The location where binaries will be installed.
435 The default is <code>$GOROOT/bin</code>.
436 After installing, you will want to arrange to add this
437 directory to your <code>$PATH</code>, so you can use the tools.
438</dd>
439
440<dt>
441<code>$GOARM</code> (arm, default=6)
442</dt>
443<dd>
Rob Pike966bf712011-03-01 13:54:22 -0800444 The ARM architecture version the run-time libraries should target.
Russ Cox86920ad2010-08-24 20:00:50 -0400445 ARMv6 cores have more efficient synchronization primitives. Setting
Rob Pike966bf712011-03-01 13:54:22 -0800446 <code>$GOARM</code> to 5 will compile the run-time libraries using
Russ Cox86920ad2010-08-24 20:00:50 -0400447 just SWP instructions that work on older architectures as well.
448 Running v6 code on an older core will cause an illegal instruction trap.
449</dd>
450</dl>
451
452<p>
453Note that <code>$GOARCH</code> and <code>$GOOS</code> identify the
454<em>target</em> environment, not the environment you are running on.
455In effect, you are always cross-compiling.
456By architecture, we mean the kind of binaries
457that the target environment can run:
458an x86-64 system running a 32-bit-only operating system
459must set <code>GOARCH</code> to <code>386</code>,
460not <code>amd64</code>.
461</p>
462
463<p>
464If you choose to override the defaults,
465set these variables in your shell profile (<code>$HOME/.bashrc</code>,
466<code>$HOME/.profile</code>, or equivalent). The settings might look
467something like this:
468</p>
469
470<pre>
471export GOROOT=$HOME/go
472export GOARCH=386
473export GOOS=linux
474</pre>