Home | History | Annotate | Download | only in doc

Lines Matching full:code

2 	"Title": "How to Write Go Code"
14 The <code>go</code> tool requires you to organize your code in a specific
25 <h2 id="Organization">Code organization</h2>
30 The <code>go</code> tool is designed to work with open source code maintained
31 in public repositories. Although you don't need to publish your code, the model
36 Go code must be kept inside a <i>workspace</i>.
41 <li><code>src</code> contains Go source files organized into packages (one package per directory),
42 <li><code>pkg</code> contains package objects, and
43 <li><code>bin</code> contains executable commands.
47 The <code>go</code> tool builds source packages and installs the resulting
48 binaries to the <code>pkg</code> and <code>bin</code> directories.
52 The <code>src</code> subdirectory typically contains multiple version control
83 This workspace contains one repository (<code>example</code>)
84 comprising two commands (<code>hello</code> and <code>outyet</code>)
85 and one library (<code>stringutil</code>).
90 packages and commands. Most Go programmers keep <i>all</i> their Go source code
100 <h3 id="GOPATH">The <code>GOPATH</code> environment variable</h3>
103 The <code>GOPATH</code> environment variable specifies the location of your
105 when developing Go code.
109 To get started, create a workspace directory and set <code>GOPATH</code>
111 <code>$HOME/work</code> in this document. Note that this must <b>not</b> be the
113 (Another common setup is to set <code>GOPATH=$HOME</code>.)
122 For convenience, add the workspace's <code>bin</code> subdirectory
123 to your <code>PATH</code>:
131 To learn more about setting up the <code>GOPATH</code> environment variable,
133 <a href="/cmd/go/#hdr-GOPATH_environment_variable"><code>go help gopath</code></a>
140 <code>"fmt"</code> and <code>"net/http"</code>.
147 If you keep your code in a source repository somewhere, then you should use the
150 <code>github.com/user</code>, that should be your base path.
154 Note that you don't need to publish your code to a remote repository before you
155 can build it. It's just a good habit to organize your code as if you will
161 We'll use <code>github.com/user</code> as our base path. Create a directory
162 inside your workspace in which to keep source code:
174 <code>github.com/user/hello</code>) and create a corresponding package directory
183 Next, create a file named <code>hello.go</code> inside that directory,
184 containing the following Go code.
198 Now you can build and install that program with the <code>go</code> tool:
207 <code>go</code> tool finds the source code by looking for the
208 <code>github.com/user/hello</code> package inside the workspace specified by
209 <code>GOPATH</code>.
213 You can also omit the package path if you run <code>go install</code> from the
223 This command builds the <code>hello</code> command, producing an executable
224 binary. It then installs that binary to the workspace's <code>bin</code>
225 directory as <code>hello</code> (or, under Windows, <code>hello.exe</code>).
226 In our example, that will be <code>$GOPATH/bin/hello</code>, which is
227 <code>$HOME/work/bin/hello</code>.
231 The <code>go</code> tool will only print output when an error occurs, so if
245 Or, as you have added <code>$GOPATH/bin</code> to your <code>PATH</code>,
257 optional: you do not need to use source control to write Go code.
272 Pushing the code to a remote repository is left as an exercise for the reader.
279 Let's write a library and use it from the <code>hello</code> program.
284 <code>github.com/user/stringutil</code>) and create the package directory:
292 Next, create a file named <code>reverse.go</code> in that directory with the
311 Now, test that the package compiles with <code>go build</code>:
327 This won't produce an output file. To do that, you must use <code>go
328 install</code>, which places the package object inside the <code>pkg</code>
333 After confirming that the <code>stringutil</code> package builds,
334 modify your original <code>hello.go</code> (which is in
335 <code>$GOPATH/src/github.com/user/hello</code>) to use it:
353 Whenever the <code>go</code> tool installs a package or binary, it also
355 So when you install the <code>hello</code> program
363 the <code>stringutil</code> package will be installed as well, automatically.
395 Note that <code>go install</code> placed the <code>stringutil.a</code> object
396 in a directory inside <code>pkg/linux_amd64</code> that mirrors its source
398 This is so that future invocations of the <code>go</code> tool can find the
400 The <code>linux_amd64</code> part is there to aid in cross-compilation,
421 where <code><i>name</i></code> is the package's default name for imports.
422 (All files in a package must use the same <code><i>name</i></code>.)
427 import path: the package imported as "<code>crypto/rot13</code>"
428 should be named <code>rot13</code>.
432 Executable commands must always use <code>package main</code>.
450 Go has a lightweight test framework composed of the <code>go test</code>
451 command and the <code>testing</code> package.
455 You write a test by creating a file with a name ending in <code>_test.go</code>
456 that contains functions named <code>TestXXX</code> with signature
457 <code>func (t *testing.T)</code>.
459 if the function calls a failure function such as <code>t.Error</code> or
460 <code>t.Fail</code>, the test is considered to have failed.
464 Add a test to the <code>stringutil</code> package by creating the file
465 <code>$GOPATH/src/github.com/user/stringutil/reverse_test.go</code> containing
466 the following Go code.
492 Then run the test with <code>go test</code>:
501 As always, if you are running the <code>go</code> tool from the package
511 Run <code><a href="/cmd/go/#hdr-Test_packages">go help test</a></code> and see the
519 An import path can describe how to obtain the package source code using a
520 revision control system such as Git or Mercurial. The <code>go</code> tool uses
524 <code><a href="https://github.com/golang/example">github.com/golang/example</a></code>.
526 <code>go get</code> will fetch, build, and install it automatically:
536 If the specified package is not present in a workspace, <code>go get</code>
537 will place it inside the first workspace specified by <code>GOPATH</code>.
538 (If the package does already exist, <code>go get</code> skips the remote
539 fetch and behaves the same as <code>go install</code>.)
543 After issuing the above <code>go get</code> command, the workspace directory
573 The <code>hello</code> command hosted at GitHub depends on the
574 <code>stringutil</code> package within the same repository. The imports in
575 <code>hello.go</code> file use the same import path convention, so the
576 <code>go get</code> command is able to locate and install the dependent
593 For more information on using remote repositories with the <code>go</code> tool, see
594 <code><a href="/cmd/go/#hdr-Remote_import_paths">go help importpath</a></code>.
608 clear, idiomatic Go code.
625 For real-time help, ask the helpful gophers in <code>#go-nuts</code> on the