~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to doc/code.html

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 
7
7
<p>
8
8
This document demonstrates the development of a simple Go package and
9
 
introduces the <a href="/cmd/go/">go command</a>, the standard way to fetch,
 
9
introduces the <a href="/cmd/go/">go tool</a>, the standard way to fetch,
10
10
build, and install Go packages and commands.
11
11
</p>
12
12
 
13
 
 
14
 
<h2 id="GOPATH">Code organization</h2>
15
 
 
16
 
<h3><code>GOPATH</code> and workspaces</h3>
17
 
 
18
 
<p>
19
 
One of Go's design goals is to make writing software easier.  To that end, the
20
 
<code>go</code> command doesn't use Makefiles or other configuration files to
21
 
guide program construction. Instead, it uses the source code to find
22
 
dependencies and determine build conditions. This means your source code and
23
 
build scripts are always in sync; they are one and the same.
24
 
</p>
25
 
 
26
 
<p>
27
 
The one thing you must do is set a <code>GOPATH</code> environment variable.
28
 
<code>GOPATH</code> tells the <code>go</code> command (and other related tools)
29
 
where to find and install the Go packages on your system.
30
 
</p>
31
 
 
32
 
<p>
33
 
<code>GOPATH</code> is a list of paths. It shares the syntax of your system's
34
 
<code>PATH</code> environment variable. A typical <code>GOPATH</code> on
35
 
a Unix system might look like this:
36
 
</p>
37
 
 
38
 
<pre>
39
 
GOPATH=/home/user/ext:/home/user/mygo
40
 
</pre>
41
 
 
42
 
<p>
43
 
(On a Windows system use semicolons as the path separator instead of colons.)
44
 
</p>
45
 
 
46
 
<p>
47
 
Each path in the list (in this case <code>/home/user/ext</code> or
48
 
<code>/home/user/mygo</code>) specifies the location of a <i>workspace</i>.
49
 
A workspace contains Go source files and their associated package objects, and
50
 
command executables. It has a prescribed structure of three subdirectories:
 
13
<p>
 
14
The <code>go</code> tool requires you to organize your code in a specific
 
15
way. Please read this document carefully.
 
16
It explains the simplest way to get up and running with your Go installation.
 
17
</p>
 
18
 
 
19
<p>
 
20
A similar explanation is available as a
 
21
<a href="http://www.youtube.com/watch?v=XCsL89YtqCs">screencast</a>.
 
22
</p>
 
23
 
 
24
 
 
25
<h2 id="Organization">Code organization</h2>
 
26
 
 
27
<h3 id="Workspaces">Workspaces</h3>
 
28
 
 
29
<p>
 
30
The <code>go</code> tool is designed to work with open source code maintained
 
31
in public repositories. Although you don't need to publish your code, the model
 
32
for how the environment is set up works the same whether you do or not.
 
33
</p>
 
34
 
 
35
<p>
 
36
Go code must be kept inside a <i>workspace</i>.
 
37
A workspace is a directory hierarchy with three directories at its root:
51
38
</p>
52
39
 
53
40
<ul>
54
 
<li><code>src</code> contains Go source files,
55
 
<li><code>pkg</code> contains compiled package objects, and
 
41
<li><code>src</code> contains Go source files organized into packages (one package per directory),
 
42
<li><code>pkg</code> contains package objects, and
56
43
<li><code>bin</code> contains executable commands.
57
44
</ul>
58
45
 
59
46
<p>
60
 
Subdirectories of the <code>src</code> directory hold independent packages, and
61
 
all source files (<code>.go</code>, <code>.c</code>, <code>.h</code>, and
62
 
<code>.s</code>) in each subdirectory are elements of that subdirectory's
63
 
package.
64
 
</p>
65
 
 
66
 
<p>
67
 
When building a program that imports the package "<code>widget</code>" the
68
 
<code>go</code> command looks for <code>src/pkg/widget</code> inside the Go root,
69
 
and then&mdash;if the package source isn't found there&mdash;it searches
70
 
for <code>src/widget</code> inside each workspace in order.
71
 
</p>
72
 
 
73
 
<p>
74
 
Multiple workspaces can offer some flexibility and convenience, but for now
75
 
we'll concern ourselves with only a single workspace.
76
 
</p>
77
 
 
78
 
<p>
79
 
Let's work through a simple example. First, create a <code>$HOME/mygo</code>
80
 
directory and its <code>src</code> subdirectory:
81
 
</p>
82
 
 
83
 
<pre>
84
 
$ mkdir -p $HOME/mygo/src # create a place to put source code
85
 
</pre>
86
 
 
87
 
<p>
88
 
Next, set it as the <code>GOPATH</code>. You should also add the
89
 
<code>bin</code> subdirectory to your <code>PATH</code> environment variable so
90
 
that you can run the commands therein without specifying their full path.
91
 
To do this, add the following lines to <code>$HOME/.profile</code> (or
92
 
equivalent):
93
 
</p>
94
 
 
95
 
<pre>
96
 
export GOPATH=$HOME/mygo
97
 
export PATH=$PATH:$HOME/mygo/bin
98
 
</pre>
99
 
 
100
 
 
101
 
<h3>Import paths</h3>
102
 
 
103
 
<p>
104
 
The standard packages are given short import paths such as <code>"fmt"</code>
105
 
and <code>"net/http"</code> for convenience. 
106
 
For your own projects, it is important to choose a base import path that is
107
 
unlikely to collide with future additions to the standard library or other
108
 
external libraries.
109
 
</p>
110
 
 
111
 
<p>
112
 
The best way to choose an import path is to use the location of your version
113
 
control repository.
114
 
For instance, if your source repository is at <code>example.com</code> 
115
 
or <code>code.google.com/p/example</code>, you should begin your package
116
 
paths with that URL, as in "<code>example.com/foo/bar</code>" or
117
 
"<code>code.google.com/p/example/foo/bar</code>".
118
 
Using this convention, the <code>go</code> command can automatically check out and
119
 
build the source code by its import path alone.
120
 
</p>
121
 
 
122
 
<p>
123
 
If you don't intend to install your code in this way, you should at
124
 
least use a unique prefix like "<code>widgets/</code>", as in
125
 
"<code>widgets/foo/bar</code>". A good rule is to use a prefix such as your
126
 
company or project name, since it is unlikely to be used by another group.
127
 
</p>
128
 
 
129
 
<p>
130
 
We'll use <code>example/</code> as our base import path:
131
 
</p>
132
 
 
133
 
<pre>
134
 
$ mkdir -p $GOPATH/src/example
135
 
</pre>
136
 
 
137
 
 
138
 
<h3>Package names</h3>
139
 
 
140
 
<p>
141
 
The first statement in a Go source file should be
 
47
The <code>go</code> tool builds source packages and installs the resulting
 
48
binaries to the <code>pkg</code> and <code>bin</code> directories.
 
49
</p>
 
50
 
 
51
<p>
 
52
The <code>src</code> subdirectory typically contains multiple version control
 
53
repositories (such as for Git or Mercurial) that track the development of one
 
54
or more source packages.
 
55
</p>
 
56
 
 
57
<p>
 
58
To give you an idea of how a workspace looks in practice, here's an example:
 
59
</p>
 
60
 
 
61
<pre>
 
62
bin/
 
63
    streak                         # command executable
 
64
    todo                           # command executable
 
65
pkg/
 
66
    linux_amd64/
 
67
        code.google.com/p/goauth2/
 
68
            oauth.a                # package object
 
69
        github.com/nf/todo/
 
70
            task.a                 # package object
 
71
src/
 
72
    code.google.com/p/goauth2/
 
73
        .hg/                       # mercurial repository metadata
 
74
        oauth/
 
75
            oauth.go               # package source
 
76
            oauth_test.go          # test source
 
77
    github.com/nf/
 
78
        streak/
 
79
            .git/                  # git repository metadata
 
80
            oauth.go               # command source
 
81
            streak.go              # command source
 
82
        todo/
 
83
            .git/                  # git repository metadata
 
84
            task/
 
85
                task.go            # package source
 
86
            todo.go                # command source
 
87
</pre>
 
88
 
 
89
<p>
 
90
This workspace contains three repositories (<code>goauth2</code>,
 
91
<code>streak</code>, and <code>todo</code>) comprising two commands
 
92
(<code>streak</code> and <code>todo</code>) and two libraries
 
93
(<code>oauth</code> and <code>task</code>).
 
94
</p>
 
95
 
 
96
<p>
 
97
Commands and libraries are built from different kinds of source packages.
 
98
We will discuss the distinction <a href="#PackageNames">later</a>.
 
99
</p>
 
100
 
 
101
 
 
102
<h3 id="GOPATH">The <code>GOPATH</code> environment variable</h3>
 
103
 
 
104
<p>
 
105
The <code>GOPATH</code> environment variable specifies the location of your
 
106
workspace. It is likely the only environment variable you'll need to set
 
107
when developing Go code.
 
108
</p>
 
109
 
 
110
<p>
 
111
To get started, create a workspace directory and set <code>GOPATH</code>
 
112
accordingly. Your workspace can be located wherever you like, but we'll use
 
113
<code>$HOME/go</code> in this document. Note that this must <b>not</b> be the
 
114
same path as your Go installation.
 
115
</p>
 
116
 
 
117
<pre>
 
118
$ <b>mkdir $HOME/go</b>
 
119
$ <b>export GOPATH=$HOME/go</b>
 
120
</pre>
 
121
 
 
122
<p>
 
123
For convenience, add the workspace's <code>bin</code> subdirectory
 
124
to your <code>PATH</code>:
 
125
</p>
 
126
 
 
127
<pre>
 
128
$ <b>export PATH=$PATH:$GOPATH/bin</b>
 
129
</pre>
 
130
 
 
131
 
 
132
<h3 id="PackagePaths">Package paths</h3>
 
133
 
 
134
<p>
 
135
The packages from the standard library are given short paths such as
 
136
<code>"fmt"</code> and <code>"net/http"</code>.
 
137
For your own packages, you must choose a base path that is unlikely to
 
138
collide with future additions to the standard library or other external
 
139
libraries.
 
140
</p>
 
141
 
 
142
<p>
 
143
If you keep your code in a source repository somewhere, then you should use the
 
144
root of that source repository as your base path.
 
145
For instance, if you have a <a href="https://github.com/">GitHub</a> account at
 
146
<code>github.com/user</code>, that should be your base path.
 
147
</p>
 
148
 
 
149
<p>
 
150
Note that you don't need to publish your code to a remote repository before you
 
151
can build it. It's just a good habit to organize your code as if you will
 
152
publish it someday. In practice you can choose any arbitrary path name,
 
153
as long as it is unique to the standard library and greater Go ecosystem.
 
154
</p>
 
155
 
 
156
<p>
 
157
We'll use <code>github.com/user</code> as our base path. Create a directory
 
158
inside your workspace in which to keep source code:
 
159
</p>
 
160
 
 
161
<pre>
 
162
$ <b>mkdir -p $GOPATH/src/github.com/user</b>
 
163
</pre>
 
164
 
 
165
 
 
166
<h3 id="Command">Your first program</h3>
 
167
 
 
168
<p>
 
169
To compile and run a simple program, first choose a package path (we'll use
 
170
<code>github.com/user/hello</code>) and create a corresponding package directory
 
171
inside your workspace:
 
172
</p>
 
173
 
 
174
<pre>
 
175
$ <b>mkdir $GOPATH/src/github.com/user/hello</b>
 
176
</pre>
 
177
 
 
178
<p>
 
179
Next, create a file named <code>hello.go</code> inside that directory,
 
180
containing the following Go code.
 
181
</p>
 
182
 
 
183
<pre>
 
184
package main
 
185
 
 
186
import "fmt"
 
187
 
 
188
func main() {
 
189
        fmt.Printf("Hello, world.\n")
 
190
}
 
191
</pre>
 
192
 
 
193
<p>
 
194
Now you can build and install that program with the <code>go</code> tool:
 
195
</p>
 
196
 
 
197
<pre>
 
198
$ <b>go install github.com/user/hello</b>
 
199
</pre>
 
200
 
 
201
<p>
 
202
Note that you can run this command from anywhere on your system. The
 
203
<code>go</code> tool finds the source code by looking for the
 
204
<code>github.com/user/hello</code> package inside the workspace specified by
 
205
<code>GOPATH</code>.
 
206
</p>
 
207
 
 
208
<p>
 
209
You can also omit the package path if you run <code>go install</code> from the
 
210
package directory:
 
211
</p>
 
212
 
 
213
<pre>
 
214
$ <b>cd $GOPATH/src/github.com/user/hello</b>
 
215
$ <b>go install</b>
 
216
</pre>
 
217
 
 
218
<p>
 
219
This command builds the <code>hello</code> command, producing an executable
 
220
binary. It then installs that binary to the workspace's <code>bin</code>
 
221
directory as <code>hello</code> (or, under Windows, <code>hello.exe</code>).
 
222
In our example, that will be <code>$GOPATH/bin/hello</code>, which is
 
223
<code>$HOME/go/bin/hello</code>.
 
224
</p>
 
225
 
 
226
<p>
 
227
The <code>go</code> tool will only print output when an error occurs, so if
 
228
these commands produce no output they have executed successfully.
 
229
</p>
 
230
 
 
231
<p>
 
232
You can now run the program by typing its full path at the command line:
 
233
</p>
 
234
 
 
235
<pre>
 
236
$ <b>$GOPATH/bin/hello</b>
 
237
Hello, world.
 
238
</pre>
 
239
 
 
240
<p>
 
241
Or, as you have added <code>$GOPATH/bin</code> to your <code>PATH</code>,
 
242
just type the binary name:
 
243
</p>
 
244
 
 
245
<pre>
 
246
$ <b>hello</b>
 
247
Hello, world.
 
248
</pre>
 
249
 
 
250
<p>
 
251
If you're using a source control system, now would be a good time to initialize
 
252
a repository, add the files, and commit your first change. Again, this step is
 
253
optional: you do not need to use source control to write Go code.
 
254
</p>
 
255
 
 
256
<pre>
 
257
$ <b>cd $GOPATH/src/github.com/user/hello</b>
 
258
$ <b>git init</b>
 
259
Initialized empty Git repository in /home/user/go/src/github.com/user/hello/.git/
 
260
$ <b>git add hello.go</b>
 
261
$ <b>git commit -m "initial commit"</b>
 
262
[master (root-commit) 0b4507d] initial commit
 
263
 1 file changed, 1 insertion(+)
 
264
  create mode 100644 hello.go
 
265
</pre>
 
266
 
 
267
<p>
 
268
Pushing the code to a remote repository is left as an exercise for the reader.
 
269
</p>
 
270
 
 
271
 
 
272
<h3 id="Library">Your first library</h3>
 
273
 
 
274
<p>
 
275
Let's write a library and use it from the <code>hello</code> program.
 
276
</p>
 
277
 
 
278
<p>
 
279
Again, the first step is to choose a package path (we'll use
 
280
<code>github.com/user/newmath</code>) and create the package directory:
 
281
</p>
 
282
 
 
283
<pre>
 
284
$ <b>mkdir $GOPATH/src/github.com/user/newmath</b>
 
285
</pre>
 
286
 
 
287
<p>
 
288
Next, create a file named <code>sqrt.go</code> in that directory with the
 
289
following contents.
 
290
</p>
 
291
 
 
292
<pre>
 
293
// Package newmath is a trivial example package.
 
294
package newmath
 
295
 
 
296
// Sqrt returns an approximation to the square root of x.
 
297
func Sqrt(x float64) float64 {
 
298
        z := 0.0
 
299
        for i := 0; i < 1000; i++ {
 
300
                z -= (z*z - x) / (2 * x)
 
301
        }
 
302
        return z
 
303
}
 
304
</pre>
 
305
 
 
306
<p>
 
307
Now, test that the package compiles with <code>go build</code>:
 
308
</p>
 
309
 
 
310
<pre>
 
311
$ <b>go build github.com/user/newmath</b>
 
312
</pre>
 
313
 
 
314
<p>
 
315
Or, if you are working in the package's source directory, just:
 
316
</p>
 
317
 
 
318
<pre>
 
319
$ <b>go build</b>
 
320
</pre>
 
321
 
 
322
<p>
 
323
This won't produce an output file. To do that, you must use <code>go
 
324
install</code>, which places the package object inside the <code>pkg</code>
 
325
directory of the workspace.
 
326
</p>
 
327
 
 
328
<p>
 
329
After confirming that the <code>newmath</code> package builds,
 
330
modify your original <code>hello.go</code> (which is in
 
331
<code>$GOPATH/src/github.com/user/hello</code>) to use it:
 
332
</p>
 
333
 
 
334
<pre>
 
335
package main
 
336
 
 
337
import (
 
338
        "fmt"
 
339
 
 
340
        <b>"github.com/user/newmath"</b>
 
341
)
 
342
 
 
343
func main() {
 
344
        fmt.Printf("Hello, world.  <b>Sqrt(2) = %v\n", newmath.Sqrt(2)</b>)
 
345
}
 
346
</pre>
 
347
 
 
348
<p>
 
349
Whenever the <code>go</code> tool installs a package or binary, it also
 
350
installs whatever dependencies it has. So when you install the <code>hello</code>
 
351
program
 
352
</p>
 
353
 
 
354
<pre>
 
355
$ <b>go install github.com/user/hello</b>
 
356
</pre>
 
357
 
 
358
<p>
 
359
the <code>newmath</code> package will be installed as well, automatically.
 
360
</p>
 
361
 
 
362
<p>
 
363
Running the new version of the program, you should see some numerical output:
 
364
</p>
 
365
 
 
366
<pre>
 
367
$ <b>hello</b>
 
368
Hello, world.  Sqrt(2) = 1.414213562373095
 
369
</pre>
 
370
 
 
371
<p>
 
372
After the steps above, your workspace should look like this:
 
373
</p>
 
374
 
 
375
<pre>
 
376
bin/
 
377
    hello              # command executable
 
378
pkg/
 
379
    linux_amd64/       # this will reflect your OS and architecture
 
380
        github.com/user/
 
381
            newmath.a  # package object
 
382
src/
 
383
    github.com/user/
 
384
        hello/
 
385
            hello.go   # command source
 
386
        newmath/
 
387
            sqrt.go    # package source
 
388
</pre>
 
389
 
 
390
<p>
 
391
Note that <code>go install</code> placed the <code>newmath.a</code> object in a
 
392
directory inside <code>pkg/linux_amd64</code> that mirrors its source
 
393
directory.
 
394
This is so that future invocations of the <code>go</code> tool can find the
 
395
package object and avoid recompiling the package unnecessarily.
 
396
The <code>linux_amd64</code> part is there to aid in cross-compilation,
 
397
and will reflect the operating system and architecture of your system.
 
398
</p>
 
399
 
 
400
<p>
 
401
Go command executables are statically linked; the package objects need not
 
402
be present to run Go programs.
 
403
</p>
 
404
 
 
405
 
 
406
<h3 id="PackageNames">Package names</h3>
 
407
 
 
408
<p>
 
409
The first statement in a Go source file must be
142
410
</p>
143
411
 
144
412
<pre>
154
422
Go's convention is that the package name is the last element of the
155
423
import path: the package imported as "<code>crypto/rot13</code>"
156
424
should be named <code>rot13</code>.
 
425
</p>
 
426
 
 
427
<p>
 
428
Executable commands must always use <code>package main</code>.
 
429
</p>
 
430
 
 
431
<p>
157
432
There is no requirement that package names be unique
158
433
across all packages linked into a single binary,
159
434
only that the import paths (their full file names) be unique.
160
435
</p>
161
436
 
162
437
<p>
163
 
Create a new package under <code>example</code> called <code>newmath</code>:
164
 
</p>
165
 
 
166
 
<pre>
167
 
$ cd $GOPATH/src/example
168
 
$ mkdir newmath
169
 
</pre>
170
 
 
171
 
<p>
172
 
Then create a file named <code>$GOPATH/src/example/newmath/sqrt.go</code>
173
 
containing the following Go code:
174
 
</p>
175
 
 
176
 
<pre>
177
 
// Package newmath is a trivial example package.
178
 
package newmath
179
 
 
180
 
// Sqrt returns an approximation to the square root of x.
181
 
func Sqrt(x float64) float64 {
182
 
        // This is a terrible implementation.
183
 
        // Real code should import "math" and use math.Sqrt.
184
 
        z := 0.0
185
 
        for i := 0; i < 1000; i++ {
186
 
                z -= (z*z - x) / (2 * x)
187
 
        }
188
 
        return z
189
 
}
190
 
</pre>
191
 
 
192
 
<p>
193
 
This package is imported by the path name of the directory it's in, starting
194
 
after the <code>src</code> component:
195
 
</p>
196
 
 
197
 
<pre>
198
 
import "example/newmath"
199
 
</pre>
200
 
 
201
 
<p>
202
438
See <a href="/doc/effective_go.html#names">Effective Go</a> to learn more about
203
439
Go's naming conventions.
204
440
</p>
205
441
 
206
442
 
207
 
<h2>Building and installing</h2>
208
 
 
209
 
<p>
210
 
The <code>go</code> command comprises several subcommands, the most central being
211
 
<code>install</code>. Running <code>go install <i>importpath</i></code> builds
212
 
and installs a package and its dependencies.
213
 
</p>
214
 
 
215
 
<p>
216
 
To "install a package" means to write the package object or executable command
217
 
to the <code>pkg</code> or <code>bin</code> subdirectory of the workspace in
218
 
which the source resides.
219
 
</p>
220
 
 
221
 
<h3>Building a package</h3>
222
 
 
223
 
<p>
224
 
To build and install the <code>newmath</code> package, type
225
 
</p>
226
 
 
227
 
<pre>
228
 
$ go install example/newmath
229
 
</pre>
230
 
 
231
 
<p>
232
 
This command will produce no output if the package and its dependencies
233
 
are built and installed correctly.
234
 
</p>
235
 
 
236
 
<p>
237
 
As a convenience, the <code>go</code> command will assume the current directory
238
 
if no import path is specified on the command line. This sequence of commands
239
 
has the same effect as the one above:
240
 
</p>
241
 
 
242
 
<pre>
243
 
$ cd $GOPATH/src/example/newmath
244
 
$ go install
245
 
</pre>
246
 
 
247
 
<p>
248
 
The resulting workspace directory tree (assuming we're running Linux on a 64-bit
249
 
system) looks like this:
250
 
</p>
251
 
 
252
 
<pre>
253
 
pkg/
254
 
    linux_amd64/
255
 
        example/
256
 
            newmath.a  # package object
257
 
src/
258
 
    example/
259
 
        newmath/
260
 
            sqrt.go    # package source
261
 
</pre>
262
 
 
263
 
 
264
 
<h3>Building a command</h3>
265
 
 
266
 
<p>
267
 
The <code>go</code> command treats code belonging to <code>package main</code> as
268
 
an executable command and installs the package binary to the
269
 
<code>GOPATH</code>'s <code>bin</code> subdirectory.
270
 
</p>
271
 
 
272
 
<p>
273
 
Add a command named <code>hello</code> to the source tree.
274
 
First create the <code>example/hello</code> directory:
275
 
</p>
276
 
 
277
 
<pre>
278
 
$ cd $GOPATH/src/example
279
 
$ mkdir hello
280
 
</pre>
281
 
 
282
 
<p>
283
 
Then create the file <code>$GOPATH/src/example/hello/hello.go</code>
284
 
containing the following Go code.
285
 
</p>
286
 
 
287
 
<pre>
288
 
// Hello is a trivial example of a main package.
289
 
package main
290
 
 
291
 
import (
292
 
        "example/newmath"
293
 
        "fmt"
294
 
)
295
 
 
296
 
func main() {
297
 
        fmt.Printf("Hello, world.  Sqrt(2) = %v\n", newmath.Sqrt(2))
298
 
}
299
 
</pre>
300
 
 
301
 
<p>
302
 
Next, run <code>go install</code>, which builds and installs the binary to
303
 
<code>$GOPATH/bin</code> (or <code>$GOBIN</code>, if set; to simplify
304
 
presentation, this document assumes <code>GOBIN</code> is unset):
305
 
</p>
306
 
 
307
 
<pre>
308
 
$ go install example/hello
309
 
</pre>
310
 
 
311
 
<p>
312
 
To run the program, invoke it by name as you would any other command:
313
 
</p>
314
 
 
315
 
<pre>
316
 
$ $GOPATH/bin/hello
317
 
Hello, world.  Sqrt(2) = 1.414213562373095
318
 
</pre>
319
 
 
320
 
<p>
321
 
If you added <code>$HOME/mygo/bin</code> to your <code>PATH</code>, you may omit
322
 
the path to the executable:
323
 
</p>
324
 
 
325
 
<pre>
326
 
$ hello
327
 
Hello, world.  Sqrt(2) = 1.414213562373095
328
 
</pre>
329
 
 
330
 
<p>
331
 
The workspace directory tree now looks like this:
332
 
</p>
333
 
 
334
 
<pre>
335
 
bin/
336
 
    hello              # command executable
337
 
pkg/
338
 
    linux_amd64/ 
339
 
        example/
340
 
            newmath.a  # package object
341
 
src/
342
 
    example/
343
 
        hello/
344
 
            hello.go   # command source
345
 
        newmath/
346
 
            sqrt.go    # package source
347
 
</pre>
348
 
 
349
 
<p>
350
 
The <code>go</code> command also provides a <code>build</code> command, which is
351
 
like <code>install</code> except it builds all objects in a temporary directory
352
 
and does not install them under <code>pkg</code> or <code>bin</code>.
353
 
When building a command an executable named after the last element of the
354
 
import path is written to the current directory. When building a package, 
355
 
<code>go build</code> serves merely to test that the package and its
356
 
dependencies can be built. (The resulting package object is thrown away.)
357
 
</p>
358
 
 
359
 
 
360
443
<h2 id="Testing">Testing</h2>
361
444
 
362
445
<p>
375
458
 
376
459
<p>
377
460
Add a test to the <code>newmath</code> package by creating the file
378
 
<code>$GOPATH/src/example/newmath/sqrt_test.go</code> containing the following
379
 
Go code.
 
461
<code>$GOPATH/src/github.com/user/newmath/sqrt_test.go</code> containing the
 
462
following Go code.
380
463
</p>
381
464
 
382
465
<pre>
388
471
        const in, out = 4, 2
389
472
        if x := Sqrt(in); x != out {
390
473
                t.Errorf("Sqrt(%v) = %v, want %v", in, x, out)
391
 
        }
 
474
        }
392
475
}
393
476
</pre>
394
477
 
395
478
<p>
396
 
Now run the test with <code>go test</code>:
397
 
</p>
398
 
 
399
 
<pre>
400
 
$ go test example/newmath
401
 
ok      example/newmath 0.165s
402
 
</pre>
403
 
 
404
 
<p>
405
 
Run <code><a href="/cmd/go/#Test_packages">go help test</a></code> and see the
 
479
Then run the test with <code>go test</code>:
 
480
</p>
 
481
 
 
482
<pre>
 
483
$ <b>go test github.com/user/newmath</b>
 
484
ok      github.com/user/newmath 0.165s
 
485
</pre>
 
486
 
 
487
<p>
 
488
As always, if you are running the <code>go</code> tool from the package
 
489
directory, you can omit the package path:
 
490
</p>
 
491
 
 
492
<pre>
 
493
$ <b>go test</b>
 
494
ok      github.com/user/newmath 0.165s
 
495
</pre>
 
496
 
 
497
<p>
 
498
Run <code><a href="/cmd/go/#hdr-Test_packages">go help test</a></code> and see the
406
499
<a href="/pkg/testing/">testing package documentation</a> for more detail.
407
500
</p>
408
501
 
411
504
 
412
505
<p>
413
506
An import path can describe how to obtain the package source code using a
414
 
revision control system such as Git or Mercurial. The <code>go</code> command uses
 
507
revision control system such as Git or Mercurial. The <code>go</code> tool uses
415
508
this property to automatically fetch packages from remote repositories.
416
509
For instance, the examples described in this document are also kept in a
417
510
Mercurial repository hosted at Google Code,
421
514
</p>
422
515
 
423
516
<pre>
424
 
$ go get code.google.com/p/go.example/hello
425
 
$ $GOPATH/bin/hello
 
517
$ <b>go get code.google.com/p/go.example/hello</b>
 
518
$ <b>$GOPATH/bin/hello</b>
426
519
Hello, world.  Sqrt(2) = 1.414213562373095
427
520
</pre>
428
521
 
435
528
 
436
529
<p>
437
530
After issuing the above <code>go get</code> command, the workspace directory
438
 
tree should now now look like this:
 
531
tree should now look like this:
439
532
</p>
440
533
 
441
534
<pre>
442
535
bin/
443
536
    hello                 # command executable
444
537
pkg/
445
 
    linux_amd64/ 
 
538
    linux_amd64/
446
539
        code.google.com/p/go.example/
447
540
            newmath.a     # package object
448
 
        example/
 
541
        github.com/user/
449
542
            newmath.a     # package object
450
543
src/
451
544
    code.google.com/p/go.example/
454
547
        newmath/
455
548
            sqrt.go       # package source
456
549
            sqrt_test.go  # test source
457
 
    example/
 
550
    github.com/user/
458
551
        hello/
459
552
            hello.go      # command source
460
553
        newmath/
476
569
<p>
477
570
This convention is the easiest way to make your Go packages available for
478
571
others to use.
479
 
The <a href="http://godashboard.appspot.com">Go Project Dashboard</a>
480
 
is a list of external Go projects including programs and libraries.
 
572
The <a href="http://code.google.com/p/go-wiki/wiki/Projects">Go Wiki</a>
 
573
and <a href="http://godoc.org/">godoc.org</a>
 
574
provide lists of external Go projects.
481
575
</p>
482
576
 
483
577
<p>
484
 
For more information on using remote repositories with the <code>go</code> command, see
485
 
<code><a href="/cmd/go/#Remote_import_path_syntax">go help remote</a></code>.
 
578
For more information on using remote repositories with the <code>go</code> tool, see
 
579
<code><a href="/cmd/go/#hdr-Remote_import_path_syntax">go help remote</a></code>.
486
580
</p>
487
581
 
488
582