blob: d8fa8b468ce230cac8278fa6d7074586fa28bde3 [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>
57Except for things like low-level operating system interface code, the runtime
58support 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,
83<li><tt>make</tt>,
84<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 Gerrand6ab8d242010-09-16 14:01:02 +100094<p>On Ubuntu/Debian, use <code>sudo apt-get install bison ed gawk gcc libc6-dev make</code>.
95</p>
96
97<h2 id="mercurial">Install Mercurial, if needed</h2>
98
99<p>
Robert Griesemer686490c2010-09-29 12:34:24 -0700100To 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 +1000101</p>
102<pre>
103sudo easy_install mercurial
104</pre>
105(On Ubuntu/Debian, you might try <code>apt-get install python-setuptools
106python-dev build-essential</code> first. The Mercurial in your distribution's
107package repository will most likely be old and broken.)
108</p>
109<p>
110If 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 -0800111</p>
112
Robert Griesemer68b40fb2011-02-02 15:53:32 -0800113<p>
114Mercurial versions 1.7.x and up require the configuration of
115<a href="http://mercurial.selenic.com/wiki/CACertificates">Certification Authorities</a>
116(CAs). Error messages of the form:
117</p>
118<pre>
119warning: go.googlecode.com certificate with fingerprint b1:af: ... bc not verified (check hostfingerprints or web.cacerts config setting)
120</pre>
121<p>
122when using Mercurial indicate that the CAs are missing.
123Check your Mercurial version (<code>hg --version</code>) and
124<a href="http://mercurial.selenic.com/wiki/CACertificates#Configuration_of_HTTPS_certificate_authorities">configure the CAs</a>
125if necessary.
126</p>
127
Russ Cox86920ad2010-08-24 20:00:50 -0400128<h2 id="fetch">Fetch the repository</h2>
Russ Coxfb39a4d2009-10-23 15:24:08 -0700129
130<p>
Russ Cox86920ad2010-08-24 20:00:50 -0400131<p>Go will install to a directory named <code>go</code>.
132Change to the directory that will be its parent
133and make sure the <code>go</code> directory does not exist.
Russ Coxfb39a4d2009-10-23 15:24:08 -0700134Then check out the repository:</p>
135
Russ Coxfb39a4d2009-10-23 15:24:08 -0700136<pre>
Russ Cox86920ad2010-08-24 20:00:50 -0400137$ hg clone -r release https://go.googlecode.com/hg/ go
Russ Coxfb39a4d2009-10-23 15:24:08 -0700138</pre>
139
Russ Cox86920ad2010-08-24 20:00:50 -0400140<h2 id="install">Install Go</h2>
Russ Coxfb39a4d2009-10-23 15:24:08 -0700141
Russ Coxfb39a4d2009-10-23 15:24:08 -0700142<p>
Devon H. O'Dell857d4cf2009-12-11 15:14:09 -0800143To build the Go distribution, run
Russ Coxfb39a4d2009-10-23 15:24:08 -0700144</p>
145
146<pre>
Russ Cox86920ad2010-08-24 20:00:50 -0400147$ cd go/src
Christopher Wedgwood34191d92010-01-25 00:09:46 -0800148$ ./all.bash
Russ Coxfb39a4d2009-10-23 15:24:08 -0700149</pre>
150
151<p>
Russ Coxda392d92010-08-18 10:08:49 -0400152If all goes well, it will finish by printing output like:
Russ Coxfb39a4d2009-10-23 15:24:08 -0700153</p>
154
155<pre>
Russ Coxb9f94762011-02-14 09:27:02 -0500156ALL TESTS PASSED
Russ Coxda392d92010-08-18 10:08:49 -0400157
158---
Russ Cox86920ad2010-08-24 20:00:50 -0400159Installed Go for linux/amd64 in /home/you/go.
160Installed commands in /home/you/go/bin.
161*** You need to add /home/you/go/bin to your $PATH. ***
162The compiler is 6g.
Russ Coxfb39a4d2009-10-23 15:24:08 -0700163</pre>
164
165<p>
Russ Coxda392d92010-08-18 10:08:49 -0400166where <var>N</var> is a number that varies from release to release
Russ Cox86920ad2010-08-24 20:00:50 -0400167and the details on the last few lines will reflect the operating system,
Russ Coxda392d92010-08-18 10:08:49 -0400168architecture, and root directory used during the install.
Russ Coxfb39a4d2009-10-23 15:24:08 -0700169</p>
170
Andrew Gerrand6ab8d242010-09-16 14:01:02 +1000171<div class="detail">
172
Russ Cox86920ad2010-08-24 20:00:50 -0400173<p>For more information about ways to control the build,
174see the discussion of <a href="#environment">environment variables</a> below.</p>
Andrew Gerrand6ab8d242010-09-16 14:01:02 +1000175</div>
Russ Cox86920ad2010-08-24 20:00:50 -0400176
177<h2 id="writing">Writing programs</h2>
Russ Coxfb39a4d2009-10-23 15:24:08 -0700178
179<p>
180Given a file <code>file.go</code>, compile it using
181</p>
182
183<pre>
184$ 6g file.go
185</pre>
186
187<p>
188<code>6g</code> is the Go compiler for <code>amd64</code>; it will write the output
189in <code>file.6</code>. The &lsquo;<code>6</code>&rsquo; identifies
190files for the <code>amd64</code> architecture.
191The identifier letters for <code>386</code> and <code>arm</code>
192are &lsquo;<code>8</code>&rsquo; and &lsquo;<code>5</code>&rsquo;.
193That is, if you were compiling for <code>386</code>, you would use
194<code>8g</code> and the output would be named <code>file.8</code>.
195</p>
196
197<p>
198To link the file, use
199</p>
200
201<pre>
202$ 6l file.6
203</pre>
204
205<p>
206and to run it
207</p>
208
209<pre>
210$ ./6.out
211</pre>
212
213<p>A complete example:
214</p>
215
216<pre>
217$ cat &gt;hello.go &lt;&lt;EOF
218package main
219
220import "fmt"
221
222func main() {
223 fmt.Printf("hello, world\n")
224}
225EOF
226$ 6g hello.go
227$ 6l hello.6
228$ ./6.out
229hello, world
230$
231</pre>
232
233<p>
234There is no need to list <code>hello.6</code>'s package dependencies
235(in this case, package <code>fmt</code>) on the <code>6l</code>
236command line.
237The linker learns about them by reading <code>hello.6</code>.
238</p>
239
Andrew Gerrand6ab8d242010-09-16 14:01:02 +1000240<div class="detail">
Russ Coxfb39a4d2009-10-23 15:24:08 -0700241<p>
242To build more complicated programs, you will probably
Russ Cox6fff7b22009-11-08 22:12:51 -0800243want to use a
Russ Coxfb39a4d2009-10-23 15:24:08 -0700244<code>Makefile</code>.
Rob Pike5f50a812009-11-05 15:07:42 -0800245There are examples in places like
Russ Cox86920ad2010-08-24 20:00:50 -0400246<code>go/src/cmd/godoc/Makefile</code>
247and <code>go/src/pkg/*/Makefile</code>.
Rob Pike5f50a812009-11-05 15:07:42 -0800248The
249<a href="contribute.html">document</a>
250about contributing to the Go project
251gives more detail about
Russ Coxfb39a4d2009-10-23 15:24:08 -0700252the process of building and testing Go programs.
253</p>
Andrew Gerrand6ab8d242010-09-16 14:01:02 +1000254</div>
255
256<h2 id="next">What's next</h2>
257
258<p>
259Start by reading the <a href="go_tutorial.html">Go Tutorial</a>.
260</p>
261
262<p>
263Build a web application by following the <a href="codelab/wiki/">Wiki
264Codelab</a>.
265</p>
266
267<p>
268Read <a href="effective_go.html">Effective Go</a> to learn about writing
269idiomatic Go code.
270</p>
271
272<p>
273For the full story, consult Go's extensive
274<a href="docs.html">documentation</a>.
275</p>
Russ Cox8b04cef2009-11-08 23:38:30 -0800276
Russ Cox86920ad2010-08-24 20:00:50 -0400277<h2 id="releases">Keeping up with releases</h2>
Russ Cox32696472009-12-15 19:16:56 -0800278
279<p>New releases are announced on the <a href="http://groups.google.com/group/golang-nuts">Go Nuts</a> mailing list.
280To update an existing tree to the latest release, you can run:
281</p>
282
283<pre>
Russ Cox86920ad2010-08-24 20:00:50 -0400284$ cd go/src
Russ Cox32696472009-12-15 19:16:56 -0800285$ hg pull
286$ hg update release
Russ Cox05f26362010-01-28 18:18:40 -0800287$ ./all.bash
Russ Cox32696472009-12-15 19:16:56 -0800288</pre>
289
Russ Cox86920ad2010-08-24 20:00:50 -0400290<h2 id="community">Community resources</h2>
Russ Cox8b04cef2009-11-08 23:38:30 -0800291
292<p>
293For real-time help, there may be users or developers on
294<code>#go-nuts</code> on the <a href="http://freenode.net/">Freenode</a> IRC server.
295</p>
296
297<p>
298The official mailing list for discussion of the Go language is
299<a href="http://groups.google.com/group/golang-nuts">Go Nuts</a>.
300</p>
301
302<p>
303Bugs can be reported using the <a href="http://code.google.com/p/go/issues/list">Go issue tracker</a>.
304</p>
305
306<p>
307For those who wish to keep up with development,
308there is another mailing list, <a href="http://groups.google.com/group/golang-checkins">golang-checkins</a>,
309that receives a message summarizing each checkin to the Go repository.
310</p>
311
Russ Cox86920ad2010-08-24 20:00:50 -0400312<h2 id="environment">Environment variables</h2>
Russ Cox8b04cef2009-11-08 23:38:30 -0800313
Russ Cox86920ad2010-08-24 20:00:50 -0400314<p>
Andrew Gerrandf89050d2010-12-05 12:04:15 +0900315The Go compilation environment can be customized by environment variables.
Russ Cox86920ad2010-08-24 20:00:50 -0400316None are required by the build, but you may wish to set them
317to override the defaults.
318</p>
319
320<dl>
321<dt>
322 <code>$GOROOT</code>
323</dt>
324<dd>
325 The root of the Go tree, often <code>$HOME/go</code>.
326 This defaults to the parent of the directory where <code>all.bash</code> is run.
327 If you choose not to set <code>$GOROOT</code>, you must
328 run <code>gomake</code> instead of <code>make</code> or <code>gmake</code>
329 when developing Go programs using the conventional makefiles.
330</dd>
331
332<dt>
333 <code>$GOROOT_FINAL</code>
334</dt>
335<dd>
336 The value assumed by installed binaries and scripts when
337 <code>$GOROOT</code> is not set.
338 It defaults to the value used for <code>$GOROOT</code>.
339 If you want to build the Go tree in one location
340 but move it elsewhere after the build, set
341 <code>$GOROOT_FINAL</code> to the eventual location.
342</dd>
343
344<dt>
345<code>$GOOS</code> and <code>$GOARCH</code>
346</dt>
347<dd>
348 The name of the target operating system and compilation architecture.
Andrew Gerrandf89050d2010-12-05 12:04:15 +0900349 These default to the values of <code>$GOHOSTOS</code> and
350 <code>$GOHOSTARCH</code> respectively (described below).
Russ Cox86920ad2010-08-24 20:00:50 -0400351
352 <p>
353 Choices for <code>$GOOS</code> are <code>linux</code>,
354 <code>freebsd</code>,
355 <code>darwin</code> (Mac OS X 10.5 or 10.6),
Peter Mundyc17394df2010-12-08 20:27:45 +1100356 and <code>windows</code> (Windows, an incomplete port).
Russ Cox86920ad2010-08-24 20:00:50 -0400357 Choices for <code>$GOARCH</code> are <code>amd64</code> (64-bit x86, the most mature port),
358 <code>386</code> (32-bit x86), and
359 <code>arm</code> (32-bit ARM, an incomplete port).
360 The valid combinations of <code>$GOOS</code> and <code>$GOARCH</code> are:
Russ Cox86920ad2010-08-24 20:00:50 -0400361 <table cellpadding="0">
362 <tr>
363 <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>
364 </tr>
365 <tr>
366 <td></td><td><code>darwin</code></td> <td><code>386</code></td>
367 </tr>
368 <tr>
369 <td></td><td><code>darwin</code></td> <td><code>amd64</code></td>
370 </tr>
371 <tr>
372 <td></td><td><code>freebsd</code></td> <td><code>386</code></td>
373 </tr>
374 <tr>
375 <td></td><td><code>freebsd</code></td> <td><code>amd64</code></td>
376 </tr>
377 <tr>
378 <td></td><td><code>linux</code></td> <td><code>386</code></td>
379 </tr>
380 <tr>
381 <td></td><td><code>linux</code></td> <td><code>amd64</code></td>
382 </tr>
383 <tr>
384 <td></td><td><code>linux</code></td> <td><code>arm</code></td> <td><i>incomplete</i></td>
385 </tr>
386 <tr>
Russ Cox86920ad2010-08-24 20:00:50 -0400387 <td></td><td><code>windows</code></td> <td><code>386</code></td> <td><i>incomplete</i></td>
388 </tr>
389 </table>
390</dd>
391
392<dt>
Andrew Gerrandf89050d2010-12-05 12:04:15 +0900393<code>$GOHOSTOS</code> and <code>$GOHOSTARCH</code>
394</dt>
395<dd>
396 The name of the host operating system and compilation architecture.
397 These default to the local system's operating system and
398 architecture.
399
400 <p>
401 Valid choices are the same as for <code>$GOOS</code> and
402 <code>$GOARCH</code>, listed above.
403 The specified values must be compatible with the local system.
404 For example, you should not set <code>$GOHOSTARCH</code> to
405 <code>arm</code> on an x86 system.
406</dd>
407
408<dt>
Russ Cox86920ad2010-08-24 20:00:50 -0400409<code>$GOBIN</code>
410</dt>
411<dd>
412 The location where binaries will be installed.
413 The default is <code>$GOROOT/bin</code>.
414 After installing, you will want to arrange to add this
415 directory to your <code>$PATH</code>, so you can use the tools.
416</dd>
417
418<dt>
419<code>$GOARM</code> (arm, default=6)
420</dt>
421<dd>
422 The ARM architecture version the runtime libraries should target.
423 ARMv6 cores have more efficient synchronization primitives. Setting
424 <code>$GOARM</code> to 5 will compile the runtime libraries using
425 just SWP instructions that work on older architectures as well.
426 Running v6 code on an older core will cause an illegal instruction trap.
427</dd>
428</dl>
429
430<p>
431Note that <code>$GOARCH</code> and <code>$GOOS</code> identify the
432<em>target</em> environment, not the environment you are running on.
433In effect, you are always cross-compiling.
434By architecture, we mean the kind of binaries
435that the target environment can run:
436an x86-64 system running a 32-bit-only operating system
437must set <code>GOARCH</code> to <code>386</code>,
438not <code>amd64</code>.
439</p>
440
441<p>
442If you choose to override the defaults,
443set these variables in your shell profile (<code>$HOME/.bashrc</code>,
444<code>$HOME/.profile</code>, or equivalent). The settings might look
445something like this:
446</p>
447
448<pre>
449export GOROOT=$HOME/go
450export GOARCH=386
451export GOOS=linux
452</pre>