go_command.html 11 KB

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