]> Cypherpunks.ru repositories - gostls13.git/blob - doc/articles/go_command.html
246b8c956d1e43294cf4043d2dabf71420f16441
[gostls13.git] / doc / articles / go_command.html
1 <!--{
2         "title": "About the go command"
3 }-->
4
5 <p>The Go distribution includes a command, named
6 "<code><a href="/cmd/go/">go</a></code>", that
7 automates the downloading, building, installation, and testing of Go packages
8 and commands.  This document talks about why we wrote a new command, what it
9 is, what it's not, and how to use it.</p>
10
11 <h2>Motivation</h2>
12
13 <p>You might have seen early Go talks in which Rob Pike jokes that the idea
14 for Go arose while waiting for a large Google server to compile.  That
15 really was the motivation for Go: to build a language that worked well
16 for building the large software that Google writes and runs. It was
17 clear from the start that such a language must provide a way to
18 express dependencies between code libraries clearly, hence the package
19 grouping and the explicit import blocks.  It was also clear from the
20 start that you might want arbitrary syntax for describing the code
21 being imported; this is why import paths are string literals.</p>
22
23 <p>An explicit goal for Go from the beginning was to be able to build Go
24 code using only the information found in the source itself, not
25 needing to write a makefile or one of the many modern replacements for
26 makefiles.  If Go needed a configuration file to explain how to build
27 your program, then Go would have failed.</p>
28
29 <p>At first, there was no Go compiler, and the initial development
30 focused on building one and then building libraries for it. For
31 expedience, we postponed the automation of building Go code by using
32 make and writing makefiles.  When compiling a single package involved
33 multiple invocations of the Go compiler, we even used a program to
34 write the makefiles for us.  You can find it if you dig through the
35 repository history.</p>
36
37 <p>The purpose of the new go command is our return to this ideal, that Go
38 programs should compile without configuration or additional effort on
39 the part of the developer beyond writing the necessary import
40 statements.</p>
41
42 <h2>Configuration versus convention</h2>
43
44 <p>The way to achieve the simplicity of a configuration-free system is to
45 establish conventions. The system works only to the extent that those conventions
46 are followed. When we first launched Go, many people published packages that
47 had to be installed in certain places, under certain names, using certain build
48 tools, in order to be used. That's understandable: that's the way it works in
49 most other languages. Over the last few years we consistently reminded people
50 about the <code>goinstall</code> command
51 (now replaced by <a href="/cmd/go/#hdr-Download_and_install_packages_and_dependencies"><code>go get</code></a>)
52 and its conventions: first, that the import path is derived in a known way from
53 the URL of the source code; second, that the place to store the sources in
54 the local file system is derived in a known way from the import path; third,
55 that each directory in a source tree corresponds to a single package; and
56 fourth, that the package is built using only information in the source code.
57 Today, the vast majority of packages follow these conventions.
58 The Go ecosystem is simpler and more powerful as a result.</p>
59
60 <p>We received many requests to allow a makefile in a package directory to
61 provide just a little extra configuration beyond what's in the source code.
62 But that would have introduced new rules. Because we did not accede to such
63 requests, we were able to write the go command and eliminate our use of make
64 or any other build system.</p>
65
66 <p>It is important to understand that the go command is not a general
67 build tool. It cannot be configured and it does not attempt to build
68 anything but Go packages.  These are important simplifying
69 assumptions: they simplify not only the implementation but also, more
70 important, the use of the tool itself.</p>
71
72 <h2>Go's conventions</h2>
73
74 <p>The <code>go</code> command requires that code adheres to a few key,
75 well-established conventions.</p>
76
77 <p>First, the import path is derived in an known way from the URL of the
78 source code.  For Bitbucket, GitHub, Google Code, and Launchpad, the
79 root directory of the repository is identified by the repository's
80 main URL, without the <code>http://</code> prefix.  Subdirectories are named by
81 adding to that path.  For example, the supplemental networking
82 libraries for Go are obtained by running</p>
83
84 <pre>
85 hg clone http://code.google.com/p/go.net
86 </pre>
87
88 <p>and thus the import path for the root directory of that repository is
89 "<code>code.google.com/p/go.net</code>".  The websocket package is stored in a
90 subdirectory, so its import path is
91 "<code>code.google.com/p/go.net/websocket</code>".</p>
92
93 <p>These paths are on the long side, but in exchange we get an
94 automatically managed name space for import paths and the ability for
95 a tool like the go command to look at an unfamiliar import path and
96 deduce where to obtain the source code.</p>
97
98 <p>Second, the place to store sources in the local file system is derived
99 in a known way from the import path.  Specifically, the first choice
100 is <code>$GOPATH/src/&lt;import-path&gt;</code>.  If <code>$GOPATH</code> is
101 unset, the go command will fall back to storing source code alongside the
102 standard Go packages, in <code>$GOROOT/src/&lt;import-path&gt;</code>.
103 If <code>$GOPATH</code> is set to a list of paths, the go command tries
104 <code>&lt;dir&gt;/src/&lt;import-path&gt;</code> for each of the directories in
105 that list.</p>
106
107 <p>Each of those trees contains, by convention, a top-level directory named
108 "<code>bin</code>", for holding compiled executables, and a top-level directory
109 named "<code>pkg</code>", for holding compiled packages that can be imported,
110 and the "<code>src</code>" directory, for holding package source files.
111 Imposing this structure lets us keep each of these directory trees
112 self-contained: the compiled form and the sources are always near each
113 other.</p>
114
115 <p>These naming conventions also let us work in the reverse direction,
116 from a directory name to its import path. This mapping is important
117 for many of the go command's subcommands, as we'll see below.</p>
118
119 <p>Third, each directory in a source tree corresponds to a single
120 package. By restricting a directory to a single package, we don't have
121 to create hybrid import paths that specify first the directory and
122 then the package within that directory.  Also, most file management
123 tools and UIs work on  directories as fundamental units.  Tying the
124 fundamental Go unit&mdash;the package&mdash;to file system structure means
125 that file system tools become Go package tools.  Copying, moving, or
126 deleting a package corresponds to copying, moving, or deleting a
127 directory.</p>
128
129 <p>Fourth, each package is built using only the information present in
130 the source files.  This makes it much more likely that the tool will
131 be able to adapt to changing build environments and conditions. For
132 example, if we allowed extra configuration such as compiler flags or
133 command line recipes, then that configuration would need to be updated
134 each time the build tools changed; it would also be inherently tied
135 to the use of a specific tool chain.</p>
136
137 <h2>Getting started with the go command</h2>
138
139 <p>Finally, a quick tour of how to use the go command, to supplement
140 the information in <a href="/doc/code.html">How to Write Go Code</a>,
141 which you might want to read first.  Assuming you want
142 to keep your source code separate from the Go distribution source
143 tree, the first step is to set <code>$GOPATH</code>, the one piece of global
144 configuration that the go command needs.  The <code>$GOPATH</code> can be a
145 list of directories, but by far the most common usage should be to set it to a
146 single directory.  In particular, you do not need a separate entry in
147 <code>$GOPATH</code> for each of your projects.  One <code>$GOPATH</code> can
148 support many projects.</p>
149
150 <p>Here’s an example.  Let’s say we decide to keep our Go code in the directory
151 <code>$HOME/mygo</code>.  We need to create that directory and set
152 <code>$GOPATH</code> accordingly.</p>
153
154 <pre>
155 $ mkdir $HOME/mygo
156 $ export GOPATH=$HOME/mygo
157 $
158 </pre>
159
160 <p>Into this directory, we now add some source code.  Suppose we want to use
161 the indexing library from the codesearch project along with a left-leaning
162 red-black tree.  We can install both with the "<code>go get</code>"
163 subcommand:</p>
164
165 <pre>
166 $ go get code.google.com/p/codesearch/index
167 $ go get github.com/petar/GoLLRB/llrb
168 $
169 </pre>
170
171 <p>Both of these projects are now downloaded and installed into our
172 <code>$GOPATH</code> directory. The one tree now contains the two directories
173 <code>src/code.google.com/p/codesearch/index/</code> and
174 <code>src/github.com/petar/GoLLRB/llrb/</code>, along with the compiled
175 packages (in <code>pkg/</code>) for those libraries and their dependencies.</p>
176
177 <p>Because we used version control systems (Mercurial and Git) to check
178 out the sources, the source tree also contains the other files in the
179 corresponding repositories, such as related packages. The "<code>go list</code>"
180 subcommand lists the import paths corresponding to its arguments, and
181 the pattern "<code>./...</code>" means start in the current directory
182 ("<code>./</code>") and find all packages below that directory
183 ("<code>...</code>"):</p>
184
185 <pre>
186 $ go list ./...
187 code.google.com/p/codesearch/cmd/cgrep
188 code.google.com/p/codesearch/cmd/cindex
189 code.google.com/p/codesearch/cmd/csearch
190 code.google.com/p/codesearch/index
191 code.google.com/p/codesearch/regexp
192 code.google.com/p/codesearch/sparse
193 github.com/petar/GoLLRB/example
194 github.com/petar/GoLLRB/llrb
195 $
196 </pre>
197
198 <p>We can also test those packages:</p>
199
200 <pre>
201 $ go test ./...
202 ?       code.google.com/p/codesearch/cmd/cgrep   [no test files]
203 ?       code.google.com/p/codesearch/cmd/cindex  [no test files]
204 ?       code.google.com/p/codesearch/cmd/csearch [no test files]
205 ok      code.google.com/p/codesearch/index       0.239s
206 ok      code.google.com/p/codesearch/regexp      0.021s
207 ?       code.google.com/p/codesearch/sparse      [no test files]
208 ?       github.com/petar/GoLLRB/example          [no test files]
209 ok      github.com/petar/GoLLRB/llrb             0.231s
210 $
211 </pre>
212
213 <p>If a go subcommand is invoked with no paths listed, it operates on the
214 current directory:</p>
215
216 <pre>
217 $ cd $GOPATH/src/code.google.com/p/codesearch/regexp
218 $ go list
219 code.google.com/p/codesearch/regexp
220 $ go test -v
221 === RUN TestNstateEnc
222 --- PASS: TestNstateEnc (0.00 seconds)
223 === RUN TestMatch
224 --- PASS: TestMatch (0.01 seconds)
225 === RUN TestGrep
226 --- PASS: TestGrep (0.00 seconds)
227 PASS
228 ok      code.google.com/p/codesearch/regexp     0.021s
229 $ go install
230 $
231 </pre>
232
233 <p>That "<code>go install</code>" subcommand installs the latest copy of the
234 package into the pkg directory. Because the go command can analyze the
235 dependency graph, "<code>go install</code>" also installs any packages that
236 this package imports but that are out of date, recursively.</p>
237
238 <p>Notice that "<code>go install</code>" was able to determine the name of the
239 import path for the package in the current directory, because of the convention
240 for directory naming.  It would be a little more convenient if we could pick
241 the name of the directory where we kept source code, and we probably wouldn't
242 pick such a long name, but that ability would require additional configuration
243 and complexity in the tool. Typing an extra directory name or two is a small
244 price to pay for the increased simplicity and power.</p>
245
246 <p>As the example shows, it’s fine to work with packages from many different
247 projects at once within a single <code>$GOPATH</code> root directory.</p>
248
249 <h2>Limitations</h2>
250
251 <p>As mentioned above, the go command is not a general-purpose build
252 tool. In particular, it does not have any facility for generating Go
253 source files during a build.  Instead, if you want to use a tool like
254 yacc or the protocol buffer compiler, you will need to write a
255 makefile (or a configuration file for the build tool of your choice)
256 to generate the Go files and then check those generated source files
257 into your repository. This is more work for you, the package author,
258 but it is significantly less work for your users, who can use
259 "<code>go get</code>" without needing to obtain and build
260 any additional tools.</p>
261
262 <h2>More information</h2>
263
264 <p>For more information, read <a href="/doc/code.html">How to Write Go Code</a>
265 and see the <a href="/cmd/go/">go command documentation</a>.</p>