# 如何开发GO代码

### 简介

GO自带的工具要求你以特定方式组织代码。 请仔细阅读本文档。 它解释了入门GO开发最简单的方法。

### 代码组织

#### 概述

• GO程序员通常将它们所有的GO代码放在一个独立的工作区中。
• 工作区包含许多版本控制存储库（例如由Git管理）。
• 每个仓库包含一个或多个包。
• 每个包由位于同一个文件夹中的一个或多个GO源代码文件组成
• 包目录的路径决定其导入路径。

#### 工作区

• src 包含GO源代码文件的目录
• pkg 包含包对象（编译后的包文件）
• bin 包含可执行的命令（生成的可执行文件）

go工具编译源码包并将生成的结果二进制文件安装到pkg目录和bin目录

src目录的子目录通常包含多个版本控制库，以此来跟踪一个或多个源码包的开发

#### 环境变量 GOPATH

go工具只在出现错误时打印错误输出，因此如果这些命令没有产生输出，则表示它们已成功执行。



stringutil也会被自动安装。

$hello Hello, Go!  在执行了上面的步骤后你的工作空间可能看起来是这样的: 注意，go installstringutil.a对象放在pkg/ linux_amd64内的目录中，该目录反映其源目录。 这样在以后调用go工具时就可以找到包对象，并避免重新编译包。 linux_amd64部分有助于交叉编译，并且将反映系统的操作系统和体系结构。 #### 包名 go源代码文件的第一行代码必须是： package name  where name is the package’s default name for imports. (All files in a package must use the same name.) Go’s convention is that the package name is the last element of the import path: the package imported as “crypto/rot13” should be named rot13. Executable commands must always use package main. There is no requirement that package names be unique across all packages linked into a single binary, only that the import paths (their full file names) be unique. See Effective Go to learn more about Go’s naming conventions. ### 测试 Go has a lightweight test framework composed of the go test command and the testing package. You write a test by creating a file with a name ending in _test.go that contains functions named TestXXX with signature func (t *testing.T). The test framework runs each such function; if the function calls a failure function such as t.Error or t.Fail, the test is considered to have failed. Add a test to the stringutil package by creating the file$GOPATH/src/github.com/user/stringutil/reverse_test.go containing the following Go code.

Then run the test with go test:

$go test github.com/user/stringutil ok github.com/user/stringutil 0.165s  As always, if you are running the go tool from the package directory, you can omit the package path: $ go test
ok      github.com/user/stringutil 0.165s


Run go help test and see the testing package documentation for more detail.

### Remote packages

An import path can describe how to obtain the package source code using a revision control system such as Git or Mercurial. The go tool uses this property to automatically fetch packages from remote repositories. For instance, the examples described in this document are also kept in a Git repository hosted at GitHub github.com/golang/example. If you include the repository URL in the package’s import path, go get will fetch, build, and install it automatically:

If the specified package is not present in a workspace, go get will place it inside the first workspace specified by GOPATH. (If the package does already exist, go get skips the remote fetch and behaves the same as go install.)

After issuing the above go get command, the workspace directory tree should now look like this:

The hello command hosted at GitHub depends on the stringutil package within the same repository. The imports in hello.go file use the same import path convention, so the go get command is able to locate and install the dependent package, too.

import "github.com/golang/example/stringutil"


This convention is the easiest way to make your Go packages available for others to use. The Go Wiki and godoc.org provide lists of external Go projects.

For more information on using remote repositories with the go tool, see go help importpath.

### What’s next

Subscribe to the golang-announce mailing list to be notified when a new stable version of Go is released.

See Effective Go for tips on writing clear, idiomatic Go code.

Take A Tour of Go to learn the language proper.

Visit the documentation page for a set of in-depth articles about the Go language and its libraries and tools.

### Getting help

For real-time help, ask the helpful gophers in #go-nuts on the Freenode IRC server.

The official mailing list for discussion of the Go language is Go Nuts.

Report bugs using the Go issue tracker.