tmpl.go 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import "html/template"
  6. var pushTmpl = template.Must(template.New("serverpush").Parse(`
  7. <!DOCTYPE html>
  8. <html>
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  11. <meta name="viewport" content="width=device-width, initial-scale=1">
  12. <meta name="theme-color" content="#375EAB">
  13. <title>HTTP/2 Server Push Demo</title>
  14. <link type="text/css" rel="stylesheet" href="/serverpush/static/style.css?{{.CacheBust}}">
  15. <script>
  16. window.initFuncs = [];
  17. </script>
  18. <script>
  19. function showtimes() {
  20. var times = 'DOM loaded: ' + (window.performance.timing.domContentLoadedEventEnd - window.performance.timing.navigationStart) + 'ms, '
  21. times += 'DOM complete (all loaded): ' + (window.performance.timing.domComplete - window.performance.timing.navigationStart) + 'ms, '
  22. times += 'Load event fired: ' + (window.performance.timing.loadEventStart - window.performance.timing.navigationStart) + 'ms'
  23. document.getElementById('loadtimes').innerHTML = times
  24. }
  25. </script>
  26. </head>
  27. <body onload="showtimes()">
  28. <div style="background:#fff9a4;padding:10px">
  29. Note: This page exists for demonstration purposes. For the actual cmd/go docs, go to <a href="golang.org/cmd/go">golang.org/cmd/go</a>.
  30. </div>
  31. <div style="padding:20px">
  32. <a href="https://{{.HTTPSHost}}/serverpush">HTTP/2 with Server Push</a> | <a href="{{.HTTP1Prefix}}/serverpush">HTTP only</a>
  33. <div id="loadtimes"></div>
  34. </div>
  35. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  36. ...
  37. </div><!-- #lowframe -->
  38. <div id="topbar" class="wide"><div class="container">
  39. <div class="top-heading" id="heading-wide"><a href="/">The Go Programming Language</a></div>
  40. <div class="top-heading" id="heading-narrow"><a href="/">Go</a></div>
  41. <a href="#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  42. <form method="GET" action="/search">
  43. <div id="menu">
  44. <a href="/doc/">Documents</a>
  45. <a href="/pkg/">Packages</a>
  46. <a href="/project/">The Project</a>
  47. <a href="/help/">Help</a>
  48. <a href="/blog/">Blog</a>
  49. <a id="playgroundButton" href="http://play.golang.org/" title="Show Go Playground">Play</a>
  50. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  51. </div>
  52. </form>
  53. </div></div>
  54. <div id="playground" class="play">
  55. <div class="input"><textarea class="code" spellcheck="false">package main
  56. import "fmt"
  57. func main() {
  58. fmt.Println("Hello, 世界")
  59. }</textarea></div>
  60. <div class="output"></div>
  61. <div class="buttons">
  62. <a class="run" title="Run this code [shift-enter]">Run</a>
  63. <a class="fmt" title="Format this code">Format</a>
  64. <a class="share" title="Share this code">Share</a>
  65. </div>
  66. </div>
  67. <div id="page" class="wide">
  68. <div class="container">
  69. <h1>Command go</h1>
  70. <div id="nav"></div>
  71. <!--
  72. Copyright 2009 The Go Authors. All rights reserved.
  73. Use of this source code is governed by a BSD-style
  74. license that can be found in the LICENSE file.
  75. -->
  76. <!--
  77. Note: Static (i.e., not template-generated) href and id
  78. attributes start with "pkg-" to make it impossible for
  79. them to conflict with generated attributes (some of which
  80. correspond to Go identifiers).
  81. -->
  82. <script type='text/javascript'>
  83. document.ANALYSIS_DATA = null;
  84. document.CALLGRAPH = null;
  85. </script>
  86. <p>
  87. Go is a tool for managing Go source code.
  88. </p>
  89. <p>
  90. Usage:
  91. </p>
  92. <pre>go command [arguments]
  93. </pre>
  94. <p>
  95. The commands are:
  96. </p>
  97. <pre>build compile packages and dependencies
  98. clean remove object files
  99. doc show documentation for package or symbol
  100. env print Go environment information
  101. bug start a bug report
  102. fix run go tool fix on packages
  103. fmt run gofmt on package sources
  104. generate generate Go files by processing source
  105. get download and install packages and dependencies
  106. install compile and install packages and dependencies
  107. list list packages
  108. run compile and run Go program
  109. test test packages
  110. tool run specified go tool
  111. version print Go version
  112. vet run go tool vet on packages
  113. </pre>
  114. <p>
  115. Use &#34;go help [command]&#34; for more information about a command.
  116. </p>
  117. <p>
  118. Additional help topics:
  119. </p>
  120. <pre>c calling between Go and C
  121. buildmode description of build modes
  122. filetype file types
  123. gopath GOPATH environment variable
  124. environment environment variables
  125. importpath import path syntax
  126. packages description of package lists
  127. testflag description of testing flags
  128. testfunc description of testing functions
  129. </pre>
  130. <p>
  131. Use &#34;go help [topic]&#34; for more information about that topic.
  132. </p>
  133. <h3 id="hdr-Compile_packages_and_dependencies">Compile packages and dependencies</h3>
  134. <p>
  135. Usage:
  136. </p>
  137. <pre>go build [-o output] [-i] [build flags] [packages]
  138. </pre>
  139. <p>
  140. Build compiles the packages named by the import paths,
  141. along with their dependencies, but it does not install the results.
  142. </p>
  143. <p>
  144. If the arguments to build are a list of .go files, build treats
  145. them as a list of source files specifying a single package.
  146. </p>
  147. <p>
  148. When compiling a single main package, build writes
  149. the resulting executable to an output file named after
  150. the first source file (&#39;go build ed.go rx.go&#39; writes &#39;ed&#39; or &#39;ed.exe&#39;)
  151. or the source code directory (&#39;go build unix/sam&#39; writes &#39;sam&#39; or &#39;sam.exe&#39;).
  152. The &#39;.exe&#39; suffix is added when writing a Windows executable.
  153. </p>
  154. <p>
  155. When compiling multiple packages or a single non-main package,
  156. build compiles the packages but discards the resulting object,
  157. serving only as a check that the packages can be built.
  158. </p>
  159. <p>
  160. When compiling packages, build ignores files that end in &#39;_test.go&#39;.
  161. </p>
  162. <p>
  163. The -o flag, only allowed when compiling a single package,
  164. forces build to write the resulting executable or object
  165. to the named output file, instead of the default behavior described
  166. in the last two paragraphs.
  167. </p>
  168. <p>
  169. The -i flag installs the packages that are dependencies of the target.
  170. </p>
  171. <p>
  172. The build flags are shared by the build, clean, get, install, list, run,
  173. and test commands:
  174. </p>
  175. <pre>-a
  176. force rebuilding of packages that are already up-to-date.
  177. -n
  178. print the commands but do not run them.
  179. -p n
  180. the number of programs, such as build commands or
  181. test binaries, that can be run in parallel.
  182. The default is the number of CPUs available.
  183. -race
  184. enable data race detection.
  185. Supported only on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64.
  186. -msan
  187. enable interoperation with memory sanitizer.
  188. Supported only on linux/amd64,
  189. and only with Clang/LLVM as the host C compiler.
  190. -v
  191. print the names of packages as they are compiled.
  192. -work
  193. print the name of the temporary work directory and
  194. do not delete it when exiting.
  195. -x
  196. print the commands.
  197. -asmflags &#39;flag list&#39;
  198. arguments to pass on each go tool asm invocation.
  199. -buildmode mode
  200. build mode to use. See &#39;go help buildmode&#39; for more.
  201. -compiler name
  202. name of compiler to use, as in runtime.Compiler (gccgo or gc).
  203. -gccgoflags &#39;arg list&#39;
  204. arguments to pass on each gccgo compiler/linker invocation.
  205. -gcflags &#39;arg list&#39;
  206. arguments to pass on each go tool compile invocation.
  207. -installsuffix suffix
  208. a suffix to use in the name of the package installation directory,
  209. in order to keep output separate from default builds.
  210. If using the -race flag, the install suffix is automatically set to race
  211. or, if set explicitly, has _race appended to it. Likewise for the -msan
  212. flag. Using a -buildmode option that requires non-default compile flags
  213. has a similar effect.
  214. -ldflags &#39;flag list&#39;
  215. arguments to pass on each go tool link invocation.
  216. -linkshared
  217. link against shared libraries previously created with
  218. -buildmode=shared.
  219. -pkgdir dir
  220. install and load all packages from dir instead of the usual locations.
  221. For example, when building with a non-standard configuration,
  222. use -pkgdir to keep generated packages in a separate location.
  223. -tags &#39;tag list&#39;
  224. a list of build tags to consider satisfied during the build.
  225. For more information about build tags, see the description of
  226. build constraints in the documentation for the go/build package.
  227. -toolexec &#39;cmd args&#39;
  228. a program to use to invoke toolchain programs like vet and asm.
  229. For example, instead of running asm, the go command will run
  230. &#39;cmd args /path/to/asm &lt;arguments for asm&gt;&#39;.
  231. </pre>
  232. <p>
  233. The list flags accept a space-separated list of strings. To embed spaces
  234. in an element in the list, surround it with either single or double quotes.
  235. </p>
  236. <p>
  237. For more about specifying packages, see &#39;go help packages&#39;.
  238. For more about where packages and binaries are installed,
  239. run &#39;go help gopath&#39;.
  240. For more about calling between Go and C/C++, run &#39;go help c&#39;.
  241. </p>
  242. <p>
  243. Note: Build adheres to certain conventions such as those described
  244. by &#39;go help gopath&#39;. Not all projects can follow these conventions,
  245. however. Installations that have their own conventions or that use
  246. a separate software build system may choose to use lower-level
  247. invocations such as &#39;go tool compile&#39; and &#39;go tool link&#39; to avoid
  248. some of the overheads and design decisions of the build tool.
  249. </p>
  250. <p>
  251. See also: go install, go get, go clean.
  252. </p>
  253. <h3 id="hdr-Remove_object_files">Remove object files</h3>
  254. <p>
  255. Usage:
  256. </p>
  257. <pre>go clean [-i] [-r] [-n] [-x] [build flags] [packages]
  258. </pre>
  259. <p>
  260. Clean removes object files from package source directories.
  261. The go command builds most objects in a temporary directory,
  262. so go clean is mainly concerned with object files left by other
  263. tools or by manual invocations of go build.
  264. </p>
  265. <p>
  266. Specifically, clean removes the following files from each of the
  267. source directories corresponding to the import paths:
  268. </p>
  269. <pre>_obj/ old object directory, left from Makefiles
  270. _test/ old test directory, left from Makefiles
  271. _testmain.go old gotest file, left from Makefiles
  272. test.out old test log, left from Makefiles
  273. build.out old test log, left from Makefiles
  274. *.[568ao] object files, left from Makefiles
  275. DIR(.exe) from go build
  276. DIR.test(.exe) from go test -c
  277. MAINFILE(.exe) from go build MAINFILE.go
  278. *.so from SWIG
  279. </pre>
  280. <p>
  281. In the list, DIR represents the final path element of the
  282. directory, and MAINFILE is the base name of any Go source
  283. file in the directory that is not included when building
  284. the package.
  285. </p>
  286. <p>
  287. The -i flag causes clean to remove the corresponding installed
  288. archive or binary (what &#39;go install&#39; would create).
  289. </p>
  290. <p>
  291. The -n flag causes clean to print the remove commands it would execute,
  292. but not run them.
  293. </p>
  294. <p>
  295. The -r flag causes clean to be applied recursively to all the
  296. dependencies of the packages named by the import paths.
  297. </p>
  298. <p>
  299. The -x flag causes clean to print remove commands as it executes them.
  300. </p>
  301. <p>
  302. For more about build flags, see &#39;go help build&#39;.
  303. </p>
  304. <p>
  305. For more about specifying packages, see &#39;go help packages&#39;.
  306. </p>
  307. <h3 id="hdr-Show_documentation_for_package_or_symbol">Show documentation for package or symbol</h3>
  308. <p>
  309. Usage:
  310. </p>
  311. <pre>go doc [-u] [-c] [package|[package.]symbol[.method]]
  312. </pre>
  313. <p>
  314. Doc prints the documentation comments associated with the item identified by its
  315. arguments (a package, const, func, type, var, or method) followed by a one-line
  316. summary of each of the first-level items &#34;under&#34; that item (package-level
  317. declarations for a package, methods for a type, etc.).
  318. </p>
  319. <p>
  320. Doc accepts zero, one, or two arguments.
  321. </p>
  322. <p>
  323. Given no arguments, that is, when run as
  324. </p>
  325. <pre>go doc
  326. </pre>
  327. <p>
  328. it prints the package documentation for the package in the current directory.
  329. If the package is a command (package main), the exported symbols of the package
  330. are elided from the presentation unless the -cmd flag is provided.
  331. </p>
  332. <p>
  333. When run with one argument, the argument is treated as a Go-syntax-like
  334. representation of the item to be documented. What the argument selects depends
  335. on what is installed in GOROOT and GOPATH, as well as the form of the argument,
  336. which is schematically one of these:
  337. </p>
  338. <pre>go doc &lt;pkg&gt;
  339. go doc &lt;sym&gt;[.&lt;method&gt;]
  340. go doc [&lt;pkg&gt;.]&lt;sym&gt;[.&lt;method&gt;]
  341. go doc [&lt;pkg&gt;.][&lt;sym&gt;.]&lt;method&gt;
  342. </pre>
  343. <p>
  344. The first item in this list matched by the argument is the one whose documentation
  345. is printed. (See the examples below.) However, if the argument starts with a capital
  346. letter it is assumed to identify a symbol or method in the current directory.
  347. </p>
  348. <p>
  349. For packages, the order of scanning is determined lexically in breadth-first order.
  350. That is, the package presented is the one that matches the search and is nearest
  351. the root and lexically first at its level of the hierarchy. The GOROOT tree is
  352. always scanned in its entirety before GOPATH.
  353. </p>
  354. <p>
  355. If there is no package specified or matched, the package in the current
  356. directory is selected, so &#34;go doc Foo&#34; shows the documentation for symbol Foo in
  357. the current package.
  358. </p>
  359. <p>
  360. The package path must be either a qualified path or a proper suffix of a
  361. path. The go tool&#39;s usual package mechanism does not apply: package path
  362. elements like . and ... are not implemented by go doc.
  363. </p>
  364. <p>
  365. When run with two arguments, the first must be a full package path (not just a
  366. suffix), and the second is a symbol or symbol and method; this is similar to the
  367. syntax accepted by godoc:
  368. </p>
  369. <pre>go doc &lt;pkg&gt; &lt;sym&gt;[.&lt;method&gt;]
  370. </pre>
  371. <p>
  372. In all forms, when matching symbols, lower-case letters in the argument match
  373. either case but upper-case letters match exactly. This means that there may be
  374. multiple matches of a lower-case argument in a package if different symbols have
  375. different cases. If this occurs, documentation for all matches is printed.
  376. </p>
  377. <p>
  378. Examples:
  379. </p>
  380. <pre>go doc
  381. Show documentation for current package.
  382. go doc Foo
  383. Show documentation for Foo in the current package.
  384. (Foo starts with a capital letter so it cannot match
  385. a package path.)
  386. go doc encoding/json
  387. Show documentation for the encoding/json package.
  388. go doc json
  389. Shorthand for encoding/json.
  390. go doc json.Number (or go doc json.number)
  391. Show documentation and method summary for json.Number.
  392. go doc json.Number.Int64 (or go doc json.number.int64)
  393. Show documentation for json.Number&#39;s Int64 method.
  394. go doc cmd/doc
  395. Show package docs for the doc command.
  396. go doc -cmd cmd/doc
  397. Show package docs and exported symbols within the doc command.
  398. go doc template.new
  399. Show documentation for html/template&#39;s New function.
  400. (html/template is lexically before text/template)
  401. go doc text/template.new # One argument
  402. Show documentation for text/template&#39;s New function.
  403. go doc text/template new # Two arguments
  404. Show documentation for text/template&#39;s New function.
  405. At least in the current tree, these invocations all print the
  406. documentation for json.Decoder&#39;s Decode method:
  407. go doc json.Decoder.Decode
  408. go doc json.decoder.decode
  409. go doc json.decode
  410. cd go/src/encoding/json; go doc decode
  411. </pre>
  412. <p>
  413. Flags:
  414. </p>
  415. <pre>-c
  416. Respect case when matching symbols.
  417. -cmd
  418. Treat a command (package main) like a regular package.
  419. Otherwise package main&#39;s exported symbols are hidden
  420. when showing the package&#39;s top-level documentation.
  421. -u
  422. Show documentation for unexported as well as exported
  423. symbols and methods.
  424. </pre>
  425. <h3 id="hdr-Print_Go_environment_information">Print Go environment information</h3>
  426. <p>
  427. Usage:
  428. </p>
  429. <pre>go env [var ...]
  430. </pre>
  431. <p>
  432. Env prints Go environment information.
  433. </p>
  434. <p>
  435. By default env prints information as a shell script
  436. (on Windows, a batch file). If one or more variable
  437. names is given as arguments, env prints the value of
  438. each named variable on its own line.
  439. </p>
  440. <h3 id="hdr-Start_a_bug_report">Start a bug report</h3>
  441. <p>
  442. Usage:
  443. </p>
  444. <pre>go bug
  445. </pre>
  446. <p>
  447. Bug opens the default browser and starts a new bug report.
  448. The report includes useful system information.
  449. </p>
  450. <h3 id="hdr-Run_go_tool_fix_on_packages">Run go tool fix on packages</h3>
  451. <p>
  452. Usage:
  453. </p>
  454. <pre>go fix [packages]
  455. </pre>
  456. <p>
  457. Fix runs the Go fix command on the packages named by the import paths.
  458. </p>
  459. <p>
  460. For more about fix, see &#39;go doc cmd/fix&#39;.
  461. For more about specifying packages, see &#39;go help packages&#39;.
  462. </p>
  463. <p>
  464. To run fix with specific options, run &#39;go tool fix&#39;.
  465. </p>
  466. <p>
  467. See also: go fmt, go vet.
  468. </p>
  469. <h3 id="hdr-Run_gofmt_on_package_sources">Run gofmt on package sources</h3>
  470. <p>
  471. Usage:
  472. </p>
  473. <pre>go fmt [-n] [-x] [packages]
  474. </pre>
  475. <p>
  476. Fmt runs the command &#39;gofmt -l -w&#39; on the packages named
  477. by the import paths. It prints the names of the files that are modified.
  478. </p>
  479. <p>
  480. For more about gofmt, see &#39;go doc cmd/gofmt&#39;.
  481. For more about specifying packages, see &#39;go help packages&#39;.
  482. </p>
  483. <p>
  484. The -n flag prints commands that would be executed.
  485. The -x flag prints commands as they are executed.
  486. </p>
  487. <p>
  488. To run gofmt with specific options, run gofmt itself.
  489. </p>
  490. <p>
  491. See also: go fix, go vet.
  492. </p>
  493. <h3 id="hdr-Generate_Go_files_by_processing_source">Generate Go files by processing source</h3>
  494. <p>
  495. Usage:
  496. </p>
  497. <pre>go generate [-run regexp] [-n] [-v] [-x] [build flags] [file.go... | packages]
  498. </pre>
  499. <p>
  500. Generate runs commands described by directives within existing
  501. files. Those commands can run any process but the intent is to
  502. create or update Go source files.
  503. </p>
  504. <p>
  505. Go generate is never run automatically by go build, go get, go test,
  506. and so on. It must be run explicitly.
  507. </p>
  508. <p>
  509. Go generate scans the file for directives, which are lines of
  510. the form,
  511. </p>
  512. <pre>//go:generate command argument...
  513. </pre>
  514. <p>
  515. (note: no leading spaces and no space in &#34;//go&#34;) where command
  516. is the generator to be run, corresponding to an executable file
  517. that can be run locally. It must either be in the shell path
  518. (gofmt), a fully qualified path (/usr/you/bin/mytool), or a
  519. command alias, described below.
  520. </p>
  521. <p>
  522. Note that go generate does not parse the file, so lines that look
  523. like directives in comments or multiline strings will be treated
  524. as directives.
  525. </p>
  526. <p>
  527. The arguments to the directive are space-separated tokens or
  528. double-quoted strings passed to the generator as individual
  529. arguments when it is run.
  530. </p>
  531. <p>
  532. Quoted strings use Go syntax and are evaluated before execution; a
  533. quoted string appears as a single argument to the generator.
  534. </p>
  535. <p>
  536. Go generate sets several variables when it runs the generator:
  537. </p>
  538. <pre>$GOARCH
  539. The execution architecture (arm, amd64, etc.)
  540. $GOOS
  541. The execution operating system (linux, windows, etc.)
  542. $GOFILE
  543. The base name of the file.
  544. $GOLINE
  545. The line number of the directive in the source file.
  546. $GOPACKAGE
  547. The name of the package of the file containing the directive.
  548. $DOLLAR
  549. A dollar sign.
  550. </pre>
  551. <p>
  552. Other than variable substitution and quoted-string evaluation, no
  553. special processing such as &#34;globbing&#34; is performed on the command
  554. line.
  555. </p>
  556. <p>
  557. As a last step before running the command, any invocations of any
  558. environment variables with alphanumeric names, such as $GOFILE or
  559. $HOME, are expanded throughout the command line. The syntax for
  560. variable expansion is $NAME on all operating systems. Due to the
  561. order of evaluation, variables are expanded even inside quoted
  562. strings. If the variable NAME is not set, $NAME expands to the
  563. empty string.
  564. </p>
  565. <p>
  566. A directive of the form,
  567. </p>
  568. <pre>//go:generate -command xxx args...
  569. </pre>
  570. <p>
  571. specifies, for the remainder of this source file only, that the
  572. string xxx represents the command identified by the arguments. This
  573. can be used to create aliases or to handle multiword generators.
  574. For example,
  575. </p>
  576. <pre>//go:generate -command foo go tool foo
  577. </pre>
  578. <p>
  579. specifies that the command &#34;foo&#34; represents the generator
  580. &#34;go tool foo&#34;.
  581. </p>
  582. <p>
  583. Generate processes packages in the order given on the command line,
  584. one at a time. If the command line lists .go files, they are treated
  585. as a single package. Within a package, generate processes the
  586. source files in a package in file name order, one at a time. Within
  587. a source file, generate runs generators in the order they appear
  588. in the file, one at a time.
  589. </p>
  590. <p>
  591. If any generator returns an error exit status, &#34;go generate&#34; skips
  592. all further processing for that package.
  593. </p>
  594. <p>
  595. The generator is run in the package&#39;s source directory.
  596. </p>
  597. <p>
  598. Go generate accepts one specific flag:
  599. </p>
  600. <pre>-run=&#34;&#34;
  601. if non-empty, specifies a regular expression to select
  602. directives whose full original source text (excluding
  603. any trailing spaces and final newline) matches the
  604. expression.
  605. </pre>
  606. <p>
  607. It also accepts the standard build flags including -v, -n, and -x.
  608. The -v flag prints the names of packages and files as they are
  609. processed.
  610. The -n flag prints commands that would be executed.
  611. The -x flag prints commands as they are executed.
  612. </p>
  613. <p>
  614. For more about build flags, see &#39;go help build&#39;.
  615. </p>
  616. <p>
  617. For more about specifying packages, see &#39;go help packages&#39;.
  618. </p>
  619. <h3 id="hdr-Download_and_install_packages_and_dependencies">Download and install packages and dependencies</h3>
  620. <p>
  621. Usage:
  622. </p>
  623. <pre>go get [-d] [-f] [-fix] [-insecure] [-t] [-u] [build flags] [packages]
  624. </pre>
  625. <p>
  626. Get downloads the packages named by the import paths, along with their
  627. dependencies. It then installs the named packages, like &#39;go install&#39;.
  628. </p>
  629. <p>
  630. The -d flag instructs get to stop after downloading the packages; that is,
  631. it instructs get not to install the packages.
  632. </p>
  633. <p>
  634. The -f flag, valid only when -u is set, forces get -u not to verify that
  635. each package has been checked out from the source control repository
  636. implied by its import path. This can be useful if the source is a local fork
  637. of the original.
  638. </p>
  639. <p>
  640. The -fix flag instructs get to run the fix tool on the downloaded packages
  641. before resolving dependencies or building the code.
  642. </p>
  643. <p>
  644. The -insecure flag permits fetching from repositories and resolving
  645. custom domains using insecure schemes such as HTTP. Use with caution.
  646. </p>
  647. <p>
  648. The -t flag instructs get to also download the packages required to build
  649. the tests for the specified packages.
  650. </p>
  651. <p>
  652. The -u flag instructs get to use the network to update the named packages
  653. and their dependencies. By default, get uses the network to check out
  654. missing packages but does not use it to look for updates to existing packages.
  655. </p>
  656. <p>
  657. The -v flag enables verbose progress and debug output.
  658. </p>
  659. <p>
  660. Get also accepts build flags to control the installation. See &#39;go help build&#39;.
  661. </p>
  662. <p>
  663. When checking out a new package, get creates the target directory
  664. GOPATH/src/&lt;import-path&gt;. If the GOPATH contains multiple entries,
  665. get uses the first one. For more details see: &#39;go help gopath&#39;.
  666. </p>
  667. <p>
  668. When checking out or updating a package, get looks for a branch or tag
  669. that matches the locally installed version of Go. The most important
  670. rule is that if the local installation is running version &#34;go1&#34;, get
  671. searches for a branch or tag named &#34;go1&#34;. If no such version exists it
  672. retrieves the most recent version of the package.
  673. </p>
  674. <p>
  675. When go get checks out or updates a Git repository,
  676. it also updates any git submodules referenced by the repository.
  677. </p>
  678. <p>
  679. Get never checks out or updates code stored in vendor directories.
  680. </p>
  681. <p>
  682. For more about specifying packages, see &#39;go help packages&#39;.
  683. </p>
  684. <p>
  685. For more about how &#39;go get&#39; finds source code to
  686. download, see &#39;go help importpath&#39;.
  687. </p>
  688. <p>
  689. See also: go build, go install, go clean.
  690. </p>
  691. <h3 id="hdr-Compile_and_install_packages_and_dependencies">Compile and install packages and dependencies</h3>
  692. <p>
  693. Usage:
  694. </p>
  695. <pre>go install [build flags] [packages]
  696. </pre>
  697. <p>
  698. Install compiles and installs the packages named by the import paths,
  699. along with their dependencies.
  700. </p>
  701. <p>
  702. For more about the build flags, see &#39;go help build&#39;.
  703. For more about specifying packages, see &#39;go help packages&#39;.
  704. </p>
  705. <p>
  706. See also: go build, go get, go clean.
  707. </p>
  708. <h3 id="hdr-List_packages">List packages</h3>
  709. <p>
  710. Usage:
  711. </p>
  712. <pre>go list [-e] [-f format] [-json] [build flags] [packages]
  713. </pre>
  714. <p>
  715. List lists the packages named by the import paths, one per line.
  716. </p>
  717. <p>
  718. The default output shows the package import path:
  719. </p>
  720. <pre>bytes
  721. encoding/json
  722. github.com/gorilla/mux
  723. golang.org/x/net/html
  724. </pre>
  725. <p>
  726. The -f flag specifies an alternate format for the list, using the
  727. syntax of package template. The default output is equivalent to -f
  728. &#39;&#39;. The struct being passed to the template is:
  729. </p>
  730. <pre>type Package struct {
  731. Dir string // directory containing package sources
  732. ImportPath string // import path of package in dir
  733. ImportComment string // path in import comment on package statement
  734. Name string // package name
  735. Doc string // package documentation string
  736. Target string // install path
  737. Shlib string // the shared library that contains this package (only set when -linkshared)
  738. Goroot bool // is this package in the Go root?
  739. Standard bool // is this package part of the standard Go library?
  740. Stale bool // would &#39;go install&#39; do anything for this package?
  741. StaleReason string // explanation for Stale==true
  742. Root string // Go root or Go path dir containing this package
  743. ConflictDir string // this directory shadows Dir in $GOPATH
  744. BinaryOnly bool // binary-only package: cannot be recompiled from sources
  745. // Source files
  746. GoFiles []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles)
  747. CgoFiles []string // .go sources files that import &#34;C&#34;
  748. IgnoredGoFiles []string // .go sources ignored due to build constraints
  749. CFiles []string // .c source files
  750. CXXFiles []string // .cc, .cxx and .cpp source files
  751. MFiles []string // .m source files
  752. HFiles []string // .h, .hh, .hpp and .hxx source files
  753. FFiles []string // .f, .F, .for and .f90 Fortran source files
  754. SFiles []string // .s source files
  755. SwigFiles []string // .swig files
  756. SwigCXXFiles []string // .swigcxx files
  757. SysoFiles []string // .syso object files to add to archive
  758. TestGoFiles []string // _test.go files in package
  759. XTestGoFiles []string // _test.go files outside package
  760. // Cgo directives
  761. CgoCFLAGS []string // cgo: flags for C compiler
  762. CgoCPPFLAGS []string // cgo: flags for C preprocessor
  763. CgoCXXFLAGS []string // cgo: flags for C++ compiler
  764. CgoFFLAGS []string // cgo: flags for Fortran compiler
  765. CgoLDFLAGS []string // cgo: flags for linker
  766. CgoPkgConfig []string // cgo: pkg-config names
  767. // Dependency information
  768. Imports []string // import paths used by this package
  769. Deps []string // all (recursively) imported dependencies
  770. TestImports []string // imports from TestGoFiles
  771. XTestImports []string // imports from XTestGoFiles
  772. // Error information
  773. Incomplete bool // this package or a dependency has an error
  774. Error *PackageError // error loading package
  775. DepsErrors []*PackageError // errors loading dependencies
  776. }
  777. </pre>
  778. <p>
  779. Packages stored in vendor directories report an ImportPath that includes the
  780. path to the vendor directory (for example, &#34;d/vendor/p&#34; instead of &#34;p&#34;),
  781. so that the ImportPath uniquely identifies a given copy of a package.
  782. The Imports, Deps, TestImports, and XTestImports lists also contain these
  783. expanded imports paths. See golang.org/s/go15vendor for more about vendoring.
  784. </p>
  785. <p>
  786. The error information, if any, is
  787. </p>
  788. <pre>type PackageError struct {
  789. ImportStack []string // shortest path from package named on command line to this one
  790. Pos string // position of error (if present, file:line:col)
  791. Err string // the error itself
  792. }
  793. </pre>
  794. <p>
  795. The template function &#34;join&#34; calls strings.Join.
  796. </p>
  797. <p>
  798. The template function &#34;context&#34; returns the build context, defined as:
  799. </p>
  800. <pre>type Context struct {
  801. GOARCH string // target architecture
  802. GOOS string // target operating system
  803. GOROOT string // Go root
  804. GOPATH string // Go path
  805. CgoEnabled bool // whether cgo can be used
  806. UseAllFiles bool // use files regardless of +build lines, file names
  807. Compiler string // compiler to assume when computing target paths
  808. BuildTags []string // build constraints to match in +build lines
  809. ReleaseTags []string // releases the current release is compatible with
  810. InstallSuffix string // suffix to use in the name of the install dir
  811. }
  812. </pre>
  813. <p>
  814. For more information about the meaning of these fields see the documentation
  815. for the go/build package&#39;s Context type.
  816. </p>
  817. <p>
  818. The -json flag causes the package data to be printed in JSON format
  819. instead of using the template format.
  820. </p>
  821. <p>
  822. The -e flag changes the handling of erroneous packages, those that
  823. cannot be found or are malformed. By default, the list command
  824. prints an error to standard error for each erroneous package and
  825. omits the packages from consideration during the usual printing.
  826. With the -e flag, the list command never prints errors to standard
  827. error and instead processes the erroneous packages with the usual
  828. printing. Erroneous packages will have a non-empty ImportPath and
  829. a non-nil Error field; other information may or may not be missing
  830. (zeroed).
  831. </p>
  832. <p>
  833. For more about build flags, see &#39;go help build&#39;.
  834. </p>
  835. <p>
  836. For more about specifying packages, see &#39;go help packages&#39;.
  837. </p>
  838. <h3 id="hdr-Compile_and_run_Go_program">Compile and run Go program</h3>
  839. <p>
  840. Usage:
  841. </p>
  842. <pre>go run [build flags] [-exec xprog] gofiles... [arguments...]
  843. </pre>
  844. <p>
  845. Run compiles and runs the main package comprising the named Go source files.
  846. A Go source file is defined to be a file ending in a literal &#34;.go&#34; suffix.
  847. </p>
  848. <p>
  849. By default, &#39;go run&#39; runs the compiled binary directly: &#39;a.out arguments...&#39;.
  850. If the -exec flag is given, &#39;go run&#39; invokes the binary using xprog:
  851. </p>
  852. <pre>&#39;xprog a.out arguments...&#39;.
  853. </pre>
  854. <p>
  855. If the -exec flag is not given, GOOS or GOARCH is different from the system
  856. default, and a program named go_$GOOS_$GOARCH_exec can be found
  857. on the current search path, &#39;go run&#39; invokes the binary using that program,
  858. for example &#39;go_nacl_386_exec a.out arguments...&#39;. This allows execution of
  859. cross-compiled programs when a simulator or other execution method is
  860. available.
  861. </p>
  862. <p>
  863. For more about build flags, see &#39;go help build&#39;.
  864. </p>
  865. <p>
  866. See also: go build.
  867. </p>
  868. <h3 id="hdr-Test_packages">Test packages</h3>
  869. <p>
  870. Usage:
  871. </p>
  872. <pre>go test [build/test flags] [packages] [build/test flags &amp; test binary flags]
  873. </pre>
  874. <p>
  875. &#39;Go test&#39; automates testing the packages named by the import paths.
  876. It prints a summary of the test results in the format:
  877. </p>
  878. <pre>ok archive/tar 0.011s
  879. FAIL archive/zip 0.022s
  880. ok compress/gzip 0.033s
  881. ...
  882. </pre>
  883. <p>
  884. followed by detailed output for each failed package.
  885. </p>
  886. <p>
  887. &#39;Go test&#39; recompiles each package along with any files with names matching
  888. the file pattern &#34;*_test.go&#34;.
  889. Files whose names begin with &#34;_&#34; (including &#34;_test.go&#34;) or &#34;.&#34; are ignored.
  890. These additional files can contain test functions, benchmark functions, and
  891. example functions. See &#39;go help testfunc&#39; for more.
  892. Each listed package causes the execution of a separate test binary.
  893. </p>
  894. <p>
  895. Test files that declare a package with the suffix &#34;_test&#34; will be compiled as a
  896. separate package, and then linked and run with the main test binary.
  897. </p>
  898. <p>
  899. The go tool will ignore a directory named &#34;testdata&#34;, making it available
  900. to hold ancillary data needed by the tests.
  901. </p>
  902. <p>
  903. By default, go test needs no arguments. It compiles and tests the package
  904. with source in the current directory, including tests, and runs the tests.
  905. </p>
  906. <p>
  907. The package is built in a temporary directory so it does not interfere with the
  908. non-test installation.
  909. </p>
  910. <p>
  911. In addition to the build flags, the flags handled by &#39;go test&#39; itself are:
  912. </p>
  913. <pre>-args
  914. Pass the remainder of the command line (everything after -args)
  915. to the test binary, uninterpreted and unchanged.
  916. Because this flag consumes the remainder of the command line,
  917. the package list (if present) must appear before this flag.
  918. -c
  919. Compile the test binary to pkg.test but do not run it
  920. (where pkg is the last element of the package&#39;s import path).
  921. The file name can be changed with the -o flag.
  922. -exec xprog
  923. Run the test binary using xprog. The behavior is the same as
  924. in &#39;go run&#39;. See &#39;go help run&#39; for details.
  925. -i
  926. Install packages that are dependencies of the test.
  927. Do not run the test.
  928. -o file
  929. Compile the test binary to the named file.
  930. The test still runs (unless -c or -i is specified).
  931. </pre>
  932. <p>
  933. The test binary also accepts flags that control execution of the test; these
  934. flags are also accessible by &#39;go test&#39;. See &#39;go help testflag&#39; for details.
  935. </p>
  936. <p>
  937. For more about build flags, see &#39;go help build&#39;.
  938. For more about specifying packages, see &#39;go help packages&#39;.
  939. </p>
  940. <p>
  941. See also: go build, go vet.
  942. </p>
  943. <h3 id="hdr-Run_specified_go_tool">Run specified go tool</h3>
  944. <p>
  945. Usage:
  946. </p>
  947. <pre>go tool [-n] command [args...]
  948. </pre>
  949. <p>
  950. Tool runs the go tool command identified by the arguments.
  951. With no arguments it prints the list of known tools.
  952. </p>
  953. <p>
  954. The -n flag causes tool to print the command that would be
  955. executed but not execute it.
  956. </p>
  957. <p>
  958. For more about each tool command, see &#39;go tool command -h&#39;.
  959. </p>
  960. <h3 id="hdr-Print_Go_version">Print Go version</h3>
  961. <p>
  962. Usage:
  963. </p>
  964. <pre>go version
  965. </pre>
  966. <p>
  967. Version prints the Go version, as reported by runtime.Version.
  968. </p>
  969. <h3 id="hdr-Run_go_tool_vet_on_packages">Run go tool vet on packages</h3>
  970. <p>
  971. Usage:
  972. </p>
  973. <pre>go vet [-n] [-x] [build flags] [packages]
  974. </pre>
  975. <p>
  976. Vet runs the Go vet command on the packages named by the import paths.
  977. </p>
  978. <p>
  979. For more about vet, see &#39;go doc cmd/vet&#39;.
  980. For more about specifying packages, see &#39;go help packages&#39;.
  981. </p>
  982. <p>
  983. To run the vet tool with specific options, run &#39;go tool vet&#39;.
  984. </p>
  985. <p>
  986. The -n flag prints commands that would be executed.
  987. The -x flag prints commands as they are executed.
  988. </p>
  989. <p>
  990. For more about build flags, see &#39;go help build&#39;.
  991. </p>
  992. <p>
  993. See also: go fmt, go fix.
  994. </p>
  995. <h3 id="hdr-Calling_between_Go_and_C">Calling between Go and C</h3>
  996. <p>
  997. There are two different ways to call between Go and C/C++ code.
  998. </p>
  999. <p>
  1000. The first is the cgo tool, which is part of the Go distribution. For
  1001. information on how to use it see the cgo documentation (go doc cmd/cgo).
  1002. </p>
  1003. <p>
  1004. The second is the SWIG program, which is a general tool for
  1005. interfacing between languages. For information on SWIG see
  1006. <a href="http://swig.org/">http://swig.org/</a>. When running go build, any file with a .swig
  1007. extension will be passed to SWIG. Any file with a .swigcxx extension
  1008. will be passed to SWIG with the -c++ option.
  1009. </p>
  1010. <p>
  1011. When either cgo or SWIG is used, go build will pass any .c, .m, .s,
  1012. or .S files to the C compiler, and any .cc, .cpp, .cxx files to the C++
  1013. compiler. The CC or CXX environment variables may be set to determine
  1014. the C or C++ compiler, respectively, to use.
  1015. </p>
  1016. <h3 id="hdr-Description_of_build_modes">Description of build modes</h3>
  1017. <p>
  1018. The &#39;go build&#39; and &#39;go install&#39; commands take a -buildmode argument which
  1019. indicates which kind of object file is to be built. Currently supported values
  1020. are:
  1021. </p>
  1022. <pre>-buildmode=archive
  1023. Build the listed non-main packages into .a files. Packages named
  1024. main are ignored.
  1025. -buildmode=c-archive
  1026. Build the listed main package, plus all packages it imports,
  1027. into a C archive file. The only callable symbols will be those
  1028. functions exported using a cgo //export comment. Requires
  1029. exactly one main package to be listed.
  1030. -buildmode=c-shared
  1031. Build the listed main packages, plus all packages that they
  1032. import, into C shared libraries. The only callable symbols will
  1033. be those functions exported using a cgo //export comment.
  1034. Non-main packages are ignored.
  1035. -buildmode=default
  1036. Listed main packages are built into executables and listed
  1037. non-main packages are built into .a files (the default
  1038. behavior).
  1039. -buildmode=shared
  1040. Combine all the listed non-main packages into a single shared
  1041. library that will be used when building with the -linkshared
  1042. option. Packages named main are ignored.
  1043. -buildmode=exe
  1044. Build the listed main packages and everything they import into
  1045. executables. Packages not named main are ignored.
  1046. -buildmode=pie
  1047. Build the listed main packages and everything they import into
  1048. position independent executables (PIE). Packages not named
  1049. main are ignored.
  1050. -buildmode=plugin
  1051. Build the listed main packages, plus all packages that they
  1052. import, into a Go plugin. Packages not named main are ignored.
  1053. </pre>
  1054. <h3 id="hdr-File_types">File types</h3>
  1055. <p>
  1056. The go command examines the contents of a restricted set of files
  1057. in each directory. It identifies which files to examine based on
  1058. the extension of the file name. These extensions are:
  1059. </p>
  1060. <pre>.go
  1061. Go source files.
  1062. .c, .h
  1063. C source files.
  1064. If the package uses cgo or SWIG, these will be compiled with the
  1065. OS-native compiler (typically gcc); otherwise they will
  1066. trigger an error.
  1067. .cc, .cpp, .cxx, .hh, .hpp, .hxx
  1068. C++ source files. Only useful with cgo or SWIG, and always
  1069. compiled with the OS-native compiler.
  1070. .m
  1071. Objective-C source files. Only useful with cgo, and always
  1072. compiled with the OS-native compiler.
  1073. .s, .S
  1074. Assembler source files.
  1075. If the package uses cgo or SWIG, these will be assembled with the
  1076. OS-native assembler (typically gcc (sic)); otherwise they
  1077. will be assembled with the Go assembler.
  1078. .swig, .swigcxx
  1079. SWIG definition files.
  1080. .syso
  1081. System object files.
  1082. </pre>
  1083. <p>
  1084. Files of each of these types except .syso may contain build
  1085. constraints, but the go command stops scanning for build constraints
  1086. at the first item in the file that is not a blank line or //-style
  1087. line comment. See the go/build package documentation for
  1088. more details.
  1089. </p>
  1090. <p>
  1091. Non-test Go source files can also include a //go:binary-only-package
  1092. comment, indicating that the package sources are included
  1093. for documentation only and must not be used to build the
  1094. package binary. This enables distribution of Go packages in
  1095. their compiled form alone. See the go/build package documentation
  1096. for more details.
  1097. </p>
  1098. <h3 id="hdr-GOPATH_environment_variable">GOPATH environment variable</h3>
  1099. <p>
  1100. The Go path is used to resolve import statements.
  1101. It is implemented by and documented in the go/build package.
  1102. </p>
  1103. <p>
  1104. The GOPATH environment variable lists places to look for Go code.
  1105. On Unix, the value is a colon-separated string.
  1106. On Windows, the value is a semicolon-separated string.
  1107. On Plan 9, the value is a list.
  1108. </p>
  1109. <p>
  1110. If the environment variable is unset, GOPATH defaults
  1111. to a subdirectory named &#34;go&#34; in the user&#39;s home directory
  1112. ($HOME/go on Unix, %USERPROFILE%\go on Windows),
  1113. unless that directory holds a Go distribution.
  1114. Run &#34;go env GOPATH&#34; to see the current GOPATH.
  1115. </p>
  1116. <p>
  1117. See <a href="https://golang.org/wiki/SettingGOPATH">https://golang.org/wiki/SettingGOPATH</a> to set a custom GOPATH.
  1118. </p>
  1119. <p>
  1120. Each directory listed in GOPATH must have a prescribed structure:
  1121. </p>
  1122. <p>
  1123. The src directory holds source code. The path below src
  1124. determines the import path or executable name.
  1125. </p>
  1126. <p>
  1127. The pkg directory holds installed package objects.
  1128. As in the Go tree, each target operating system and
  1129. architecture pair has its own subdirectory of pkg
  1130. (pkg/GOOS_GOARCH).
  1131. </p>
  1132. <p>
  1133. If DIR is a directory listed in the GOPATH, a package with
  1134. source in DIR/src/foo/bar can be imported as &#34;foo/bar&#34; and
  1135. has its compiled form installed to &#34;DIR/pkg/GOOS_GOARCH/foo/bar.a&#34;.
  1136. </p>
  1137. <p>
  1138. The bin directory holds compiled commands.
  1139. Each command is named for its source directory, but only
  1140. the final element, not the entire path. That is, the
  1141. command with source in DIR/src/foo/quux is installed into
  1142. DIR/bin/quux, not DIR/bin/foo/quux. The &#34;foo/&#34; prefix is stripped
  1143. so that you can add DIR/bin to your PATH to get at the
  1144. installed commands. If the GOBIN environment variable is
  1145. set, commands are installed to the directory it names instead
  1146. of DIR/bin. GOBIN must be an absolute path.
  1147. </p>
  1148. <p>
  1149. Here&#39;s an example directory layout:
  1150. </p>
  1151. <pre>GOPATH=/home/user/go
  1152. /home/user/go/
  1153. src/
  1154. foo/
  1155. bar/ (go code in package bar)
  1156. x.go
  1157. quux/ (go code in package main)
  1158. y.go
  1159. bin/
  1160. quux (installed command)
  1161. pkg/
  1162. linux_amd64/
  1163. foo/
  1164. bar.a (installed package object)
  1165. </pre>
  1166. <p>
  1167. Go searches each directory listed in GOPATH to find source code,
  1168. but new packages are always downloaded into the first directory
  1169. in the list.
  1170. </p>
  1171. <p>
  1172. See <a href="https://golang.org/doc/code.html">https://golang.org/doc/code.html</a> for an example.
  1173. </p>
  1174. <h3 id="hdr-Internal_Directories">Internal Directories</h3>
  1175. <p>
  1176. Code in or below a directory named &#34;internal&#34; is importable only
  1177. by code in the directory tree rooted at the parent of &#34;internal&#34;.
  1178. Here&#39;s an extended version of the directory layout above:
  1179. </p>
  1180. <pre>/home/user/go/
  1181. src/
  1182. crash/
  1183. bang/ (go code in package bang)
  1184. b.go
  1185. foo/ (go code in package foo)
  1186. f.go
  1187. bar/ (go code in package bar)
  1188. x.go
  1189. internal/
  1190. baz/ (go code in package baz)
  1191. z.go
  1192. quux/ (go code in package main)
  1193. y.go
  1194. </pre>
  1195. <p>
  1196. The code in z.go is imported as &#34;foo/internal/baz&#34;, but that
  1197. import statement can only appear in source files in the subtree
  1198. rooted at foo. The source files foo/f.go, foo/bar/x.go, and
  1199. foo/quux/y.go can all import &#34;foo/internal/baz&#34;, but the source file
  1200. crash/bang/b.go cannot.
  1201. </p>
  1202. <p>
  1203. See <a href="https://golang.org/s/go14internal">https://golang.org/s/go14internal</a> for details.
  1204. </p>
  1205. <h3 id="hdr-Vendor_Directories">Vendor Directories</h3>
  1206. <p>
  1207. Go 1.6 includes support for using local copies of external dependencies
  1208. to satisfy imports of those dependencies, often referred to as vendoring.
  1209. </p>
  1210. <p>
  1211. Code below a directory named &#34;vendor&#34; is importable only
  1212. by code in the directory tree rooted at the parent of &#34;vendor&#34;,
  1213. and only using an import path that omits the prefix up to and
  1214. including the vendor element.
  1215. </p>
  1216. <p>
  1217. Here&#39;s the example from the previous section,
  1218. but with the &#34;internal&#34; directory renamed to &#34;vendor&#34;
  1219. and a new foo/vendor/crash/bang directory added:
  1220. </p>
  1221. <pre>/home/user/go/
  1222. src/
  1223. crash/
  1224. bang/ (go code in package bang)
  1225. b.go
  1226. foo/ (go code in package foo)
  1227. f.go
  1228. bar/ (go code in package bar)
  1229. x.go
  1230. vendor/
  1231. crash/
  1232. bang/ (go code in package bang)
  1233. b.go
  1234. baz/ (go code in package baz)
  1235. z.go
  1236. quux/ (go code in package main)
  1237. y.go
  1238. </pre>
  1239. <p>
  1240. The same visibility rules apply as for internal, but the code
  1241. in z.go is imported as &#34;baz&#34;, not as &#34;foo/vendor/baz&#34;.
  1242. </p>
  1243. <p>
  1244. Code in vendor directories deeper in the source tree shadows
  1245. code in higher directories. Within the subtree rooted at foo, an import
  1246. of &#34;crash/bang&#34; resolves to &#34;foo/vendor/crash/bang&#34;, not the
  1247. top-level &#34;crash/bang&#34;.
  1248. </p>
  1249. <p>
  1250. Code in vendor directories is not subject to import path
  1251. checking (see &#39;go help importpath&#39;).
  1252. </p>
  1253. <p>
  1254. When &#39;go get&#39; checks out or updates a git repository, it now also
  1255. updates submodules.
  1256. </p>
  1257. <p>
  1258. Vendor directories do not affect the placement of new repositories
  1259. being checked out for the first time by &#39;go get&#39;: those are always
  1260. placed in the main GOPATH, never in a vendor subtree.
  1261. </p>
  1262. <p>
  1263. See <a href="https://golang.org/s/go15vendor">https://golang.org/s/go15vendor</a> for details.
  1264. </p>
  1265. <h3 id="hdr-Environment_variables">Environment variables</h3>
  1266. <p>
  1267. The go command, and the tools it invokes, examine a few different
  1268. environment variables. For many of these, you can see the default
  1269. value of on your system by running &#39;go env NAME&#39;, where NAME is the
  1270. name of the variable.
  1271. </p>
  1272. <p>
  1273. General-purpose environment variables:
  1274. </p>
  1275. <pre>GCCGO
  1276. The gccgo command to run for &#39;go build -compiler=gccgo&#39;.
  1277. GOARCH
  1278. The architecture, or processor, for which to compile code.
  1279. Examples are amd64, 386, arm, ppc64.
  1280. GOBIN
  1281. The directory where &#39;go install&#39; will install a command.
  1282. GOOS
  1283. The operating system for which to compile code.
  1284. Examples are linux, darwin, windows, netbsd.
  1285. GOPATH
  1286. For more details see: &#39;go help gopath&#39;.
  1287. GORACE
  1288. Options for the race detector.
  1289. See <a href="https://golang.org/doc/articles/race_detector.html">https://golang.org/doc/articles/race_detector.html</a>.
  1290. GOROOT
  1291. The root of the go tree.
  1292. </pre>
  1293. <p>
  1294. Environment variables for use with cgo:
  1295. </p>
  1296. <pre>CC
  1297. The command to use to compile C code.
  1298. CGO_ENABLED
  1299. Whether the cgo command is supported. Either 0 or 1.
  1300. CGO_CFLAGS
  1301. Flags that cgo will pass to the compiler when compiling
  1302. C code.
  1303. CGO_CPPFLAGS
  1304. Flags that cgo will pass to the compiler when compiling
  1305. C or C++ code.
  1306. CGO_CXXFLAGS
  1307. Flags that cgo will pass to the compiler when compiling
  1308. C++ code.
  1309. CGO_FFLAGS
  1310. Flags that cgo will pass to the compiler when compiling
  1311. Fortran code.
  1312. CGO_LDFLAGS
  1313. Flags that cgo will pass to the compiler when linking.
  1314. CXX
  1315. The command to use to compile C++ code.
  1316. PKG_CONFIG
  1317. Path to pkg-config tool.
  1318. </pre>
  1319. <p>
  1320. Architecture-specific environment variables:
  1321. </p>
  1322. <pre>GOARM
  1323. For GOARCH=arm, the ARM architecture for which to compile.
  1324. Valid values are 5, 6, 7.
  1325. GO386
  1326. For GOARCH=386, the floating point instruction set.
  1327. Valid values are 387, sse2.
  1328. </pre>
  1329. <p>
  1330. Special-purpose environment variables:
  1331. </p>
  1332. <pre>GOROOT_FINAL
  1333. The root of the installed Go tree, when it is
  1334. installed in a location other than where it is built.
  1335. File names in stack traces are rewritten from GOROOT to
  1336. GOROOT_FINAL.
  1337. GO_EXTLINK_ENABLED
  1338. Whether the linker should use external linking mode
  1339. when using -linkmode=auto with code that uses cgo.
  1340. Set to 0 to disable external linking mode, 1 to enable it.
  1341. GIT_ALLOW_PROTOCOL
  1342. Defined by Git. A colon-separated list of schemes that are allowed to be used
  1343. with git fetch/clone. If set, any scheme not explicitly mentioned will be
  1344. considered insecure by &#39;go get&#39;.
  1345. </pre>
  1346. <h3 id="hdr-Import_path_syntax">Import path syntax</h3>
  1347. <p>
  1348. An import path (see &#39;go help packages&#39;) denotes a package stored in the local
  1349. file system. In general, an import path denotes either a standard package (such
  1350. as &#34;unicode/utf8&#34;) or a package found in one of the work spaces (For more
  1351. details see: &#39;go help gopath&#39;).
  1352. </p>
  1353. <h3 id="hdr-Relative_import_paths">Relative import paths</h3>
  1354. <p>
  1355. An import path beginning with ./ or ../ is called a relative path.
  1356. The toolchain supports relative import paths as a shortcut in two ways.
  1357. </p>
  1358. <p>
  1359. First, a relative path can be used as a shorthand on the command line.
  1360. If you are working in the directory containing the code imported as
  1361. &#34;unicode&#34; and want to run the tests for &#34;unicode/utf8&#34;, you can type
  1362. &#34;go test ./utf8&#34; instead of needing to specify the full path.
  1363. Similarly, in the reverse situation, &#34;go test ..&#34; will test &#34;unicode&#34; from
  1364. the &#34;unicode/utf8&#34; directory. Relative patterns are also allowed, like
  1365. &#34;go test ./...&#34; to test all subdirectories. See &#39;go help packages&#39; for details
  1366. on the pattern syntax.
  1367. </p>
  1368. <p>
  1369. Second, if you are compiling a Go program not in a work space,
  1370. you can use a relative path in an import statement in that program
  1371. to refer to nearby code also not in a work space.
  1372. This makes it easy to experiment with small multipackage programs
  1373. outside of the usual work spaces, but such programs cannot be
  1374. installed with &#34;go install&#34; (there is no work space in which to install them),
  1375. so they are rebuilt from scratch each time they are built.
  1376. To avoid ambiguity, Go programs cannot use relative import paths
  1377. within a work space.
  1378. </p>
  1379. <h3 id="hdr-Remote_import_paths">Remote import paths</h3>
  1380. <p>
  1381. Certain import paths also
  1382. describe how to obtain the source code for the package using
  1383. a revision control system.
  1384. </p>
  1385. <p>
  1386. A few common code hosting sites have special syntax:
  1387. </p>
  1388. <pre>Bitbucket (Git, Mercurial)
  1389. import &#34;bitbucket.org/user/project&#34;
  1390. import &#34;bitbucket.org/user/project/sub/directory&#34;
  1391. GitHub (Git)
  1392. import &#34;github.com/user/project&#34;
  1393. import &#34;github.com/user/project/sub/directory&#34;
  1394. Launchpad (Bazaar)
  1395. import &#34;launchpad.net/project&#34;
  1396. import &#34;launchpad.net/project/series&#34;
  1397. import &#34;launchpad.net/project/series/sub/directory&#34;
  1398. import &#34;launchpad.net/~user/project/branch&#34;
  1399. import &#34;launchpad.net/~user/project/branch/sub/directory&#34;
  1400. IBM DevOps Services (Git)
  1401. import &#34;hub.jazz.net/git/user/project&#34;
  1402. import &#34;hub.jazz.net/git/user/project/sub/directory&#34;
  1403. </pre>
  1404. <p>
  1405. For code hosted on other servers, import paths may either be qualified
  1406. with the version control type, or the go tool can dynamically fetch
  1407. the import path over https/http and discover where the code resides
  1408. from a &lt;meta&gt; tag in the HTML.
  1409. </p>
  1410. <p>
  1411. To declare the code location, an import path of the form
  1412. </p>
  1413. <pre>repository.vcs/path
  1414. </pre>
  1415. <p>
  1416. specifies the given repository, with or without the .vcs suffix,
  1417. using the named version control system, and then the path inside
  1418. that repository. The supported version control systems are:
  1419. </p>
  1420. <pre>Bazaar .bzr
  1421. Git .git
  1422. Mercurial .hg
  1423. Subversion .svn
  1424. </pre>
  1425. <p>
  1426. For example,
  1427. </p>
  1428. <pre>import &#34;example.org/user/foo.hg&#34;
  1429. </pre>
  1430. <p>
  1431. denotes the root directory of the Mercurial repository at
  1432. example.org/user/foo or foo.hg, and
  1433. </p>
  1434. <pre>import &#34;example.org/repo.git/foo/bar&#34;
  1435. </pre>
  1436. <p>
  1437. denotes the foo/bar directory of the Git repository at
  1438. example.org/repo or repo.git.
  1439. </p>
  1440. <p>
  1441. When a version control system supports multiple protocols,
  1442. each is tried in turn when downloading. For example, a Git
  1443. download tries https://, then git+ssh://.
  1444. </p>
  1445. <p>
  1446. By default, downloads are restricted to known secure protocols
  1447. (e.g. https, ssh). To override this setting for Git downloads, the
  1448. GIT_ALLOW_PROTOCOL environment variable can be set (For more details see:
  1449. &#39;go help environment&#39;).
  1450. </p>
  1451. <p>
  1452. If the import path is not a known code hosting site and also lacks a
  1453. version control qualifier, the go tool attempts to fetch the import
  1454. over https/http and looks for a &lt;meta&gt; tag in the document&#39;s HTML
  1455. &lt;head&gt;.
  1456. </p>
  1457. <p>
  1458. The meta tag has the form:
  1459. </p>
  1460. <pre>&lt;meta name=&#34;go-import&#34; content=&#34;import-prefix vcs repo-root&#34;&gt;
  1461. </pre>
  1462. <p>
  1463. The import-prefix is the import path corresponding to the repository
  1464. root. It must be a prefix or an exact match of the package being
  1465. fetched with &#34;go get&#34;. If it&#39;s not an exact match, another http
  1466. request is made at the prefix to verify the &lt;meta&gt; tags match.
  1467. </p>
  1468. <p>
  1469. The meta tag should appear as early in the file as possible.
  1470. In particular, it should appear before any raw JavaScript or CSS,
  1471. to avoid confusing the go command&#39;s restricted parser.
  1472. </p>
  1473. <p>
  1474. The vcs is one of &#34;git&#34;, &#34;hg&#34;, &#34;svn&#34;, etc,
  1475. </p>
  1476. <p>
  1477. The repo-root is the root of the version control system
  1478. containing a scheme and not containing a .vcs qualifier.
  1479. </p>
  1480. <p>
  1481. For example,
  1482. </p>
  1483. <pre>import &#34;example.org/pkg/foo&#34;
  1484. </pre>
  1485. <p>
  1486. will result in the following requests:
  1487. </p>
  1488. <pre><a href="https://example.org/pkg/foo?go-get=1">https://example.org/pkg/foo?go-get=1</a> (preferred)
  1489. <a href="http://example.org/pkg/foo?go-get=1">http://example.org/pkg/foo?go-get=1</a> (fallback, only with -insecure)
  1490. </pre>
  1491. <p>
  1492. If that page contains the meta tag
  1493. </p>
  1494. <pre>&lt;meta name=&#34;go-import&#34; content=&#34;example.org git <a href="https://code.org/r/p/exproj">https://code.org/r/p/exproj</a>&#34;&gt;
  1495. </pre>
  1496. <p>
  1497. the go tool will verify that <a href="https://example.org/?go-get=1">https://example.org/?go-get=1</a> contains the
  1498. same meta tag and then git clone <a href="https://code.org/r/p/exproj">https://code.org/r/p/exproj</a> into
  1499. GOPATH/src/example.org.
  1500. </p>
  1501. <p>
  1502. New downloaded packages are written to the first directory listed in the GOPATH
  1503. environment variable (For more details see: &#39;go help gopath&#39;).
  1504. </p>
  1505. <p>
  1506. The go command attempts to download the version of the
  1507. package appropriate for the Go release being used.
  1508. Run &#39;go help get&#39; for more.
  1509. </p>
  1510. <h3 id="hdr-Import_path_checking">Import path checking</h3>
  1511. <p>
  1512. When the custom import path feature described above redirects to a
  1513. known code hosting site, each of the resulting packages has two possible
  1514. import paths, using the custom domain or the known hosting site.
  1515. </p>
  1516. <p>
  1517. A package statement is said to have an &#34;import comment&#34; if it is immediately
  1518. followed (before the next newline) by a comment of one of these two forms:
  1519. </p>
  1520. <pre>package math // import &#34;path&#34;
  1521. package math /* import &#34;path&#34; */
  1522. </pre>
  1523. <p>
  1524. The go command will refuse to install a package with an import comment
  1525. unless it is being referred to by that import path. In this way, import comments
  1526. let package authors make sure the custom import path is used and not a
  1527. direct path to the underlying code hosting site.
  1528. </p>
  1529. <p>
  1530. Import path checking is disabled for code found within vendor trees.
  1531. This makes it possible to copy code into alternate locations in vendor trees
  1532. without needing to update import comments.
  1533. </p>
  1534. <p>
  1535. See <a href="https://golang.org/s/go14customimport">https://golang.org/s/go14customimport</a> for details.
  1536. </p>
  1537. <h3 id="hdr-Description_of_package_lists">Description of package lists</h3>
  1538. <p>
  1539. Many commands apply to a set of packages:
  1540. </p>
  1541. <pre>go action [packages]
  1542. </pre>
  1543. <p>
  1544. Usually, [packages] is a list of import paths.
  1545. </p>
  1546. <p>
  1547. An import path that is a rooted path or that begins with
  1548. a . or .. element is interpreted as a file system path and
  1549. denotes the package in that directory.
  1550. </p>
  1551. <p>
  1552. Otherwise, the import path P denotes the package found in
  1553. the directory DIR/src/P for some DIR listed in the GOPATH
  1554. environment variable (For more details see: &#39;go help gopath&#39;).
  1555. </p>
  1556. <p>
  1557. If no import paths are given, the action applies to the
  1558. package in the current directory.
  1559. </p>
  1560. <p>
  1561. There are four reserved names for paths that should not be used
  1562. for packages to be built with the go tool:
  1563. </p>
  1564. <p>
  1565. - &#34;main&#34; denotes the top-level package in a stand-alone executable.
  1566. </p>
  1567. <p>
  1568. - &#34;all&#34; expands to all package directories found in all the GOPATH
  1569. trees. For example, &#39;go list all&#39; lists all the packages on the local
  1570. system.
  1571. </p>
  1572. <p>
  1573. - &#34;std&#34; is like all but expands to just the packages in the standard
  1574. Go library.
  1575. </p>
  1576. <p>
  1577. - &#34;cmd&#34; expands to the Go repository&#39;s commands and their
  1578. internal libraries.
  1579. </p>
  1580. <p>
  1581. Import paths beginning with &#34;cmd/&#34; only match source code in
  1582. the Go repository.
  1583. </p>
  1584. <p>
  1585. An import path is a pattern if it includes one or more &#34;...&#34; wildcards,
  1586. each of which can match any string, including the empty string and
  1587. strings containing slashes. Such a pattern expands to all package
  1588. directories found in the GOPATH trees with names matching the
  1589. patterns. As a special case, x/... matches x as well as x&#39;s subdirectories.
  1590. For example, net/... expands to net and packages in its subdirectories.
  1591. </p>
  1592. <p>
  1593. An import path can also name a package to be downloaded from
  1594. a remote repository. Run &#39;go help importpath&#39; for details.
  1595. </p>
  1596. <p>
  1597. Every package in a program must have a unique import path.
  1598. By convention, this is arranged by starting each path with a
  1599. unique prefix that belongs to you. For example, paths used
  1600. internally at Google all begin with &#39;google&#39;, and paths
  1601. denoting remote repositories begin with the path to the code,
  1602. such as &#39;github.com/user/repo&#39;.
  1603. </p>
  1604. <p>
  1605. Packages in a program need not have unique package names,
  1606. but there are two reserved package names with special meaning.
  1607. The name main indicates a command, not a library.
  1608. Commands are built into binaries and cannot be imported.
  1609. The name documentation indicates documentation for
  1610. a non-Go program in the directory. Files in package documentation
  1611. are ignored by the go command.
  1612. </p>
  1613. <p>
  1614. As a special case, if the package list is a list of .go files from a
  1615. single directory, the command is applied to a single synthesized
  1616. package made up of exactly those files, ignoring any build constraints
  1617. in those files and ignoring any other files in the directory.
  1618. </p>
  1619. <p>
  1620. Directory and file names that begin with &#34;.&#34; or &#34;_&#34; are ignored
  1621. by the go tool, as are directories named &#34;testdata&#34;.
  1622. </p>
  1623. <h3 id="hdr-Description_of_testing_flags">Description of testing flags</h3>
  1624. <p>
  1625. The &#39;go test&#39; command takes both flags that apply to &#39;go test&#39; itself
  1626. and flags that apply to the resulting test binary.
  1627. </p>
  1628. <p>
  1629. Several of the flags control profiling and write an execution profile
  1630. suitable for &#34;go tool pprof&#34;; run &#34;go tool pprof -h&#34; for more
  1631. information. The --alloc_space, --alloc_objects, and --show_bytes
  1632. options of pprof control how the information is presented.
  1633. </p>
  1634. <p>
  1635. The following flags are recognized by the &#39;go test&#39; command and
  1636. control the execution of any test:
  1637. </p>
  1638. <pre>-bench regexp
  1639. Run (sub)benchmarks matching a regular expression.
  1640. The given regular expression is split into smaller ones by
  1641. top-level &#39;/&#39;, where each must match the corresponding part of a
  1642. benchmark&#39;s identifier.
  1643. By default, no benchmarks run. To run all benchmarks,
  1644. use &#39;-bench .&#39; or &#39;-bench=.&#39;.
  1645. -benchtime t
  1646. Run enough iterations of each benchmark to take t, specified
  1647. as a time.Duration (for example, -benchtime 1h30s).
  1648. The default is 1 second (1s).
  1649. -count n
  1650. Run each test and benchmark n times (default 1).
  1651. If -cpu is set, run n times for each GOMAXPROCS value.
  1652. Examples are always run once.
  1653. -cover
  1654. Enable coverage analysis.
  1655. -covermode set,count,atomic
  1656. Set the mode for coverage analysis for the package[s]
  1657. being tested. The default is &#34;set&#34; unless -race is enabled,
  1658. in which case it is &#34;atomic&#34;.
  1659. The values:
  1660. set: bool: does this statement run?
  1661. count: int: how many times does this statement run?
  1662. atomic: int: count, but correct in multithreaded tests;
  1663. significantly more expensive.
  1664. Sets -cover.
  1665. -coverpkg pkg1,pkg2,pkg3
  1666. Apply coverage analysis in each test to the given list of packages.
  1667. The default is for each test to analyze only the package being tested.
  1668. Packages are specified as import paths.
  1669. Sets -cover.
  1670. -cpu 1,2,4
  1671. Specify a list of GOMAXPROCS values for which the tests or
  1672. benchmarks should be executed. The default is the current value
  1673. of GOMAXPROCS.
  1674. -parallel n
  1675. Allow parallel execution of test functions that call t.Parallel.
  1676. The value of this flag is the maximum number of tests to run
  1677. simultaneously; by default, it is set to the value of GOMAXPROCS.
  1678. Note that -parallel only applies within a single test binary.
  1679. The &#39;go test&#39; command may run tests for different packages
  1680. in parallel as well, according to the setting of the -p flag
  1681. (see &#39;go help build&#39;).
  1682. -run regexp
  1683. Run only those tests and examples matching the regular expression.
  1684. For tests the regular expression is split into smaller ones by
  1685. top-level &#39;/&#39;, where each must match the corresponding part of a
  1686. test&#39;s identifier.
  1687. -short
  1688. Tell long-running tests to shorten their run time.
  1689. It is off by default but set during all.bash so that installing
  1690. the Go tree can run a sanity check but not spend time running
  1691. exhaustive tests.
  1692. -timeout t
  1693. If a test runs longer than t, panic.
  1694. The default is 10 minutes (10m).
  1695. -v
  1696. Verbose output: log all tests as they are run. Also print all
  1697. text from Log and Logf calls even if the test succeeds.
  1698. </pre>
  1699. <p>
  1700. The following flags are also recognized by &#39;go test&#39; and can be used to
  1701. profile the tests during execution:
  1702. </p>
  1703. <pre>-benchmem
  1704. Print memory allocation statistics for benchmarks.
  1705. -blockprofile block.out
  1706. Write a goroutine blocking profile to the specified file
  1707. when all tests are complete.
  1708. Writes test binary as -c would.
  1709. -blockprofilerate n
  1710. Control the detail provided in goroutine blocking profiles by
  1711. calling runtime.SetBlockProfileRate with n.
  1712. See &#39;go doc runtime.SetBlockProfileRate&#39;.
  1713. The profiler aims to sample, on average, one blocking event every
  1714. n nanoseconds the program spends blocked. By default,
  1715. if -test.blockprofile is set without this flag, all blocking events
  1716. are recorded, equivalent to -test.blockprofilerate=1.
  1717. -coverprofile cover.out
  1718. Write a coverage profile to the file after all tests have passed.
  1719. Sets -cover.
  1720. -cpuprofile cpu.out
  1721. Write a CPU profile to the specified file before exiting.
  1722. Writes test binary as -c would.
  1723. -memprofile mem.out
  1724. Write a memory profile to the file after all tests have passed.
  1725. Writes test binary as -c would.
  1726. -memprofilerate n
  1727. Enable more precise (and expensive) memory profiles by setting
  1728. runtime.MemProfileRate. See &#39;go doc runtime.MemProfileRate&#39;.
  1729. To profile all memory allocations, use -test.memprofilerate=1
  1730. and pass --alloc_space flag to the pprof tool.
  1731. -mutexprofile mutex.out
  1732. Write a mutex contention profile to the specified file
  1733. when all tests are complete.
  1734. Writes test binary as -c would.
  1735. -mutexprofilefraction n
  1736. Sample 1 in n stack traces of goroutines holding a
  1737. contended mutex.
  1738. -outputdir directory
  1739. Place output files from profiling in the specified directory,
  1740. by default the directory in which &#34;go test&#34; is running.
  1741. -trace trace.out
  1742. Write an execution trace to the specified file before exiting.
  1743. </pre>
  1744. <p>
  1745. Each of these flags is also recognized with an optional &#39;test.&#39; prefix,
  1746. as in -test.v. When invoking the generated test binary (the result of
  1747. &#39;go test -c&#39;) directly, however, the prefix is mandatory.
  1748. </p>
  1749. <p>
  1750. The &#39;go test&#39; command rewrites or removes recognized flags,
  1751. as appropriate, both before and after the optional package list,
  1752. before invoking the test binary.
  1753. </p>
  1754. <p>
  1755. For instance, the command
  1756. </p>
  1757. <pre>go test -v -myflag testdata -cpuprofile=prof.out -x
  1758. </pre>
  1759. <p>
  1760. will compile the test binary and then run it as
  1761. </p>
  1762. <pre>pkg.test -test.v -myflag testdata -test.cpuprofile=prof.out
  1763. </pre>
  1764. <p>
  1765. (The -x flag is removed because it applies only to the go command&#39;s
  1766. execution, not to the test itself.)
  1767. </p>
  1768. <p>
  1769. The test flags that generate profiles (other than for coverage) also
  1770. leave the test binary in pkg.test for use when analyzing the profiles.
  1771. </p>
  1772. <p>
  1773. When &#39;go test&#39; runs a test binary, it does so from within the
  1774. corresponding package&#39;s source code directory. Depending on the test,
  1775. it may be necessary to do the same when invoking a generated test
  1776. binary directly.
  1777. </p>
  1778. <p>
  1779. The command-line package list, if present, must appear before any
  1780. flag not known to the go test command. Continuing the example above,
  1781. the package list would have to appear before -myflag, but could appear
  1782. on either side of -v.
  1783. </p>
  1784. <p>
  1785. To keep an argument for a test binary from being interpreted as a
  1786. known flag or a package name, use -args (see &#39;go help test&#39;) which
  1787. passes the remainder of the command line through to the test binary
  1788. uninterpreted and unaltered.
  1789. </p>
  1790. <p>
  1791. For instance, the command
  1792. </p>
  1793. <pre>go test -v -args -x -v
  1794. </pre>
  1795. <p>
  1796. will compile the test binary and then run it as
  1797. </p>
  1798. <pre>pkg.test -test.v -x -v
  1799. </pre>
  1800. <p>
  1801. Similarly,
  1802. </p>
  1803. <pre>go test -args math
  1804. </pre>
  1805. <p>
  1806. will compile the test binary and then run it as
  1807. </p>
  1808. <pre>pkg.test math
  1809. </pre>
  1810. <p>
  1811. In the first example, the -x and the second -v are passed through to the
  1812. test binary unchanged and with no effect on the go command itself.
  1813. In the second example, the argument math is passed through to the test
  1814. binary, instead of being interpreted as the package list.
  1815. </p>
  1816. <h3 id="hdr-Description_of_testing_functions">Description of testing functions</h3>
  1817. <p>
  1818. The &#39;go test&#39; command expects to find test, benchmark, and example functions
  1819. in the &#34;*_test.go&#34; files corresponding to the package under test.
  1820. </p>
  1821. <p>
  1822. A test function is one named TestXXX (where XXX is any alphanumeric string
  1823. not starting with a lower case letter) and should have the signature,
  1824. </p>
  1825. <pre>func TestXXX(t *testing.T) { ... }
  1826. </pre>
  1827. <p>
  1828. A benchmark function is one named BenchmarkXXX and should have the signature,
  1829. </p>
  1830. <pre>func BenchmarkXXX(b *testing.B) { ... }
  1831. </pre>
  1832. <p>
  1833. An example function is similar to a test function but, instead of using
  1834. *testing.T to report success or failure, prints output to os.Stdout.
  1835. If the last comment in the function starts with &#34;Output:&#34; then the output
  1836. is compared exactly against the comment (see examples below). If the last
  1837. comment begins with &#34;Unordered output:&#34; then the output is compared to the
  1838. comment, however the order of the lines is ignored. An example with no such
  1839. comment is compiled but not executed. An example with no text after
  1840. &#34;Output:&#34; is compiled, executed, and expected to produce no output.
  1841. </p>
  1842. <p>
  1843. Godoc displays the body of ExampleXXX to demonstrate the use
  1844. of the function, constant, or variable XXX. An example of a method M with
  1845. receiver type T or *T is named ExampleT_M. There may be multiple examples
  1846. for a given function, constant, or variable, distinguished by a trailing _xxx,
  1847. where xxx is a suffix not beginning with an upper case letter.
  1848. </p>
  1849. <p>
  1850. Here is an example of an example:
  1851. </p>
  1852. <pre>func ExamplePrintln() {
  1853. Println(&#34;The output of\nthis example.&#34;)
  1854. // Output: The output of
  1855. // this example.
  1856. }
  1857. </pre>
  1858. <p>
  1859. Here is another example where the ordering of the output is ignored:
  1860. </p>
  1861. <pre>func ExamplePerm() {
  1862. for _, value := range Perm(4) {
  1863. fmt.Println(value)
  1864. }
  1865. // Unordered output: 4
  1866. // 2
  1867. // 1
  1868. // 3
  1869. // 0
  1870. }
  1871. </pre>
  1872. <p>
  1873. The entire test file is presented as the example when it contains a single
  1874. example function, at least one other function, type, variable, or constant
  1875. declaration, and no test or benchmark functions.
  1876. </p>
  1877. <p>
  1878. See the documentation of the testing package for more information.
  1879. </p>
  1880. <div id="footer">
  1881. Build version go1.8.<br>
  1882. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  1883. the content of this page is licensed under the
  1884. Creative Commons Attribution 3.0 License,
  1885. and code is licensed under a <a href="/LICENSE">BSD license</a>.<br>
  1886. <a href="/doc/tos.html">Terms of Service</a> |
  1887. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  1888. </div>
  1889. </div><!-- .container -->
  1890. </div><!-- #page -->
  1891. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  1892. <script type="text/javascript" src="/serverpush/static/jquery.min.js?{{.CacheBust}}"></script>
  1893. <script type="text/javascript" src="/serverpush/static/playground.js?{{.CacheBust}}"></script>
  1894. <script>var goVersion = "go1.8";</script>
  1895. <script type="text/javascript" src="/serverpush/static/godocs.js?{{.CacheBust}}"></script>
  1896. </body>
  1897. </html>
  1898. `))