1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
<!-- Installing Go -->
<h2>Introduction</h2>
<p>Go is an open source project, distributed under a
<a href="/LICENSE">BSD-style license</a>.
This document explains how to check out the sources,
build them on your own machine, and run them.
</p>
<p>
There are two distinct ways to experiment with Go.
This document focuses on the <code>gc</code> Go
compiler and tools (<code>6g</code>, <code>8g</code> etc.).
For information on how to use <code>gccgo</code>, a more traditional
compiler using the GCC back end, see
<a href="gccgo_install.html">Setting up and using gccgo</a>.
</p>
<h2>Environment variables</h2>
<p>
The Go compilation environment depends on three environment variables plus one optional variable:
</p>
<dl>
<dt>
<code>$GOROOT</code>
</dt>
<dd>The root of the Go tree. Typically this is <code>$HOME/go</code>
but it can be any directory.
</dd>
<dt>
<code>$GOOS</code> and <code>$GOARCH</code>
</dt>
<dd>
The name of the target operating system and compilation architecture.
Choices for <code>$GOOS</code> are <code>linux</code>,
<code>freebsd</code>,
<code>darwin</code> (Mac OS X 10.5 or 10.6),
and <code>nacl</code> (Native Client, an incomplete port).
Choices for <code>$GOARCH</code> are <code>amd64</code> (64-bit x86, the most mature port),
<code>386</code> (32-bit x86), and
<code>arm</code> (32-bit ARM, an incomplete port).
The valid combinations of <code>$GOOS</code> and <code>$GOARCH</code> are:
<p>
<table cellpadding="0">
<tr>
<th width="50"><th align="left" width="100"><code>$GOOS</code></th> <th align="left" width="100"><code>$GOARCH</code></th>
</tr>
<tr>
<td></td><td><code>darwin</code></td> <td><code>386</code></td>
</tr>
<tr>
<td></td><td><code>darwin</code></td> <td><code>amd64</code></td>
</tr>
<tr>
<td></td><td><code>freebsd</code></td> <td><code>386</code></td>
</tr>
<tr>
<td></td><td><code>freebsd</code></td> <td><code>amd64</code></td>
</tr>
<tr>
<td></td><td><code>linux</code></td> <td><code>386</code></td>
</tr>
<tr>
<td></td><td><code>linux</code></td> <td><code>amd64</code></td>
</tr>
<tr>
<td></td><td><code>linux</code></td> <td><code>arm</code></td>
</tr>
<tr>
<td></td><td><code>nacl</code></td> <td><code>386</code></td>
</tr>
</table>
<p>
</dd>
<dt>
<code>$GOBIN</code> (optional)
</dt>
<dd>
The location where binaries will be installed.
The default is <code>$HOME/bin</code>.
After installing, you will want to arrange to add this
directory to your <code>$PATH</code>, so you can use the tools.
</dd>
<dt>
<code>$GOARM</code> (optional, arm, default=6)
</dt>
<dd>
The ARM architecture version the runtime libraries should target.
ARMv6 cores have more efficient synchronization primitives. Setting
<code>$GOARM</code> to 5 will compile the runtime libraries using
just SWP instructions that work on older architectures as well.
Running v6 code on an older core will cause an illegal instruction trap.
</dd>
</dl>
<p>
Note that <code>$GOARCH</code> and <code>$GOOS</code> identify the
<em>target</em> environment, not the environment you are running on.
In effect, you are always cross-compiling.
</p>
<p>
Set these variables in your shell profile (<code>$HOME/.bashrc</code>,
<code>$HOME/.profile</code>, or equivalent). The settings might look
something like this:
</p>
<pre>
export GOROOT=$HOME/go
export GOARCH=amd64
export GOOS=linux
</pre>
<p>
Double-check them by listing your environment. (You will need to launch
a new shell or terminal window for the changes to take effect.)
</p>
<pre>
$ env | grep '^GO'
</pre>
<h2>Ports</h2>
<p>
The Go compilers support three instruction sets.
There are important differences in the quality of the compilers for the different
architectures.
</p>
<dl>
<dt>
<code>amd64</code> (a.k.a. <code>x86-64</code>); <code>6g,6l,6c,6a</code>
</dt>
<dd>
The most mature implementation. The compiler has an effective optimizer
(registerizer) and generates good code (although <code>gccgo</code>
can do noticeably better sometimes).
</dd>
<dt>
<code>386</code> (a.k.a. <code>x86</code> or <code>x86-32</code>); <code>8g,8l,8c,8a</code>
</dt>
<dd>
Comparable to the <code>amd64</code> port. Not as well soaked but
should be nearly as solid.
</dd>
<dt>
<code>arm</code> (a.k.a. <code>ARM</code>); <code>5g,5l,5c,5a</code>
</dt>
<dd>
It's got a couple of outstanding bugs but is improving.
Tested against QEMU and an android phone.
Only supports Linux binaries.
</dd>
</dl>
<p>
Except for things like low-level operating system interface code, the runtime
support is the same in all ports and includes a mark-and-sweep garbage collector
(a fancier one is in the works), efficient array and string slicing,
support for segmented stacks, and a strong goroutine implementation.
</p>
<p>
See the separate <a href="gccgo_install.html"><code>gccgo</code> document</a>
for details about that compiler and environment.
</p>
<h2>Install C tools, if needed</h2>
<p>The Go tool chain is written in C. To build it, you need
to have GCC, the standard C libraries, the parser generator Bison,
<tt>make</tt>, <tt>awk</tt>, and the text editor <tt>ed</tt> installed. On OS X, they can be
installed as part of
<a href="http://developer.apple.com/TOOLS/Xcode/">Xcode</a>. On Linux, use
</p>
<pre>
$ sudo apt-get install bison gcc libc6-dev ed gawk make
</pre>
<p>
(or the equivalent on your Linux distribution).
</p>
<h2>Fetch the repository</h2>
<p>
If you do not have Mercurial installed (you do not have an <code>hg</code> command),
this command:
</p>
<pre>
$ sudo easy_install mercurial
</pre>
<p>works on most systems.
(On Ubuntu/Debian, you might try <code>apt-get install python-setuptools python-dev build-essential gcc</code> first.)
If that fails, visit the <a href="http://mercurial.selenic.com/wiki/Download">Mercurial Download</a> page.</p>
<p>Make sure the <code>$GOROOT</code> directory does not exist or is empty.
Then check out the repository:</p>
<pre>
$ hg clone -r release https://go.googlecode.com/hg/ $GOROOT
</pre>
<h2>Install Go</h2>
<p>
To build the Go distribution, run
</p>
<pre>
$ cd $GOROOT/src
$ ./all.bash
</pre>
<p>
If all goes well, it will finish by printing
</p>
<pre>
--- cd ../test
N known bugs; 0 unexpected bugs
</pre>
<p>
where <var>N</var> is a number that varies from release to release.
</p>
<h2>Writing programs</h2>
<p>
Given a file <code>file.go</code>, compile it using
</p>
<pre>
$ 6g file.go
</pre>
<p>
<code>6g</code> is the Go compiler for <code>amd64</code>; it will write the output
in <code>file.6</code>. The ‘<code>6</code>’ identifies
files for the <code>amd64</code> architecture.
The identifier letters for <code>386</code> and <code>arm</code>
are ‘<code>8</code>’ and ‘<code>5</code>’.
That is, if you were compiling for <code>386</code>, you would use
<code>8g</code> and the output would be named <code>file.8</code>.
</p>
<p>
To link the file, use
</p>
<pre>
$ 6l file.6
</pre>
<p>
and to run it
</p>
<pre>
$ ./6.out
</pre>
<p>A complete example:
</p>
<pre>
$ cat >hello.go <<EOF
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
EOF
$ 6g hello.go
$ 6l hello.6
$ ./6.out
hello, world
$
</pre>
<p>
There is no need to list <code>hello.6</code>'s package dependencies
(in this case, package <code>fmt</code>) on the <code>6l</code>
command line.
The linker learns about them by reading <code>hello.6</code>.
</p>
<p>
To build more complicated programs, you will probably
want to use a
<code>Makefile</code>.
There are examples in places like
<code>$GOROOT/src/cmd/godoc/Makefile</code>
and <code>$GOROOT/src/pkg/*/Makefile</code>.
The
<a href="contribute.html">document</a>
about contributing to the Go project
gives more detail about
the process of building and testing Go programs.
</p>
<h2>Keeping up with releases</h2>
<p>New releases are announced on the <a href="http://groups.google.com/group/golang-nuts">Go Nuts</a> mailing list.
To update an existing tree to the latest release, you can run:
</p>
<pre>
$ cd $GOROOT/src
$ hg pull
$ hg update release
$ ./all.bash
</pre>
<h2>Community resources</h2>
<p>
For real-time help, there may be users or developers on
<code>#go-nuts</code> on the <a href="http://freenode.net/">Freenode</a> IRC server.
</p>
<p>
The official mailing list for discussion of the Go language is
<a href="http://groups.google.com/group/golang-nuts">Go Nuts</a>.
</p>
<p>
Bugs can be reported using the <a href="http://code.google.com/p/go/issues/list">Go issue tracker</a>.
</p>
<p>
For those who wish to keep up with development,
there is another mailing list, <a href="http://groups.google.com/group/golang-checkins">golang-checkins</a>,
that receives a message summarizing each checkin to the Go repository.
</p>
|