标签:des style http color os io for ar
This document demonstrates the development of a simple Go package and introduces the go tool, the standard way to fetch, build, and install Go packages and commands.
The go tool requires you to organize your code in a specific way. Please read this document carefully. It explains the simplest way to get up and running with your Go installation.
A similar explanation is available as a screencast.
The go tool is designed to work with open source code maintained in public repositories. Although you don‘t need to publish your code, the model for how the environment is set up works the same whether you do or not.
Go code must be kept inside a workspace. A workspace is a directory hierarchy with three directories at its root:
The go tool builds source packages and installs the resulting binaries to the pkg and bin directories.
The src subdirectory typically contains multiple version control repositories (such as for Git or Mercurial) that track the development of one or more source packages.
To give you an idea of how a workspace looks in practice, here‘s an example:
bin/ streak # command executable todo # command executable pkg/ linux_amd64/ code.google.com/p/goauth2/ oauth.a # package object github.com/nf/todo/ task.a # package object src/ code.google.com/p/goauth2/ .hg/ # mercurial repository metadata oauth/ oauth.go # package source oauth_test.go # test source github.com/nf/ streak/ .git/ # git repository metadata oauth.go # command source streak.go # command source todo/ .git/ # git repository metadata task/ task.go # package source todo.go # command source
This workspace contains three repositories (goauth2, streak, and todo) comprising two commands (streak and todo) and two libraries (oauth and task).
Commands and libraries are built from different kinds of source packages. We will discuss the distinction later.
The GOPATH environment variable specifies the location of your workspace. It is likely the only environment variable you‘ll need to set when developing Go code.
To get started, create a workspace directory and set GOPATH accordingly. Your workspace can be located wherever you like, but we‘ll use $HOME/go in this document. Note that this mustnot be the same path as your Go installation.
$ mkdir $HOME/go $ export GOPATH=$HOME/go
For convenience, add the workspace‘s bin subdirectory to your PATH:
$ export PATH=$PATH:$GOPATH/bin
The packages from the standard library are given short paths such as "fmt" and "net/http". For your own packages, you must choose a base path that is unlikely to collide with future additions to the standard library or other external libraries.
If you keep your code in a source repository somewhere, then you should use the root of that source repository as your base path. For instance, if you have a GitHub account atgithub.com/user, that should be your base path.
Note that you don‘t need to publish your code to a remote repository before you can build it. It‘s just a good habit to organize your code as if you will publish it someday. In practice you can choose any arbitrary path name, as long as it is unique to the standard library and greater Go ecosystem.
We‘ll use github.com/user as our base path. Create a directory inside your workspace in which to keep source code:
$ mkdir -p $GOPATH/src/github.com/user
To compile and run a simple program, first choose a package path (we‘ll use github.com/user/hello) and create a corresponding package directory inside your workspace:
$ mkdir $GOPATH/src/github.com/user/hello
Next, create a file named hello.go inside that directory, containing the following Go code.
package main import "fmt" func main() { fmt.Printf("Hello, world.\n") }
Now you can build and install that program with the go tool:
$ go install github.com/user/hello
Note that you can run this command from anywhere on your system. The go tool finds the source code by looking for the github.com/user/hello package inside the workspace specified by GOPATH.
You can also omit the package path if you run go install from the package directory:
$ cd $GOPATH/src/github.com/user/hello $ go install
This command builds the hello command, producing an executable binary. It then installs that binary to the workspace‘s bin directory as hello (or, under Windows, hello.exe). In our example, that will be $GOPATH/bin/hello, which is $HOME/go/bin/hello.
The go tool will only print output when an error occurs, so if these commands produce no output they have executed successfully.
You can now run the program by typing its full path at the command line:
$ $GOPATH/bin/hello Hello, world.
Or, as you have added $GOPATH/bin to your PATH, just type the binary name:
$ hello Hello, world.
If you‘re using a source control system, now would be a good time to initialize a repository, add the files, and commit your first change. Again, this step is optional: you do not need to use source control to write Go code.
$ cd $GOPATH/src/github.com/user/hello $ git init Initialized empty Git repository in /home/user/go/src/github.com/user/hello/.git/ $ git add hello.go $ git commit -m "initial commit" [master (root-commit) 0b4507d] initial commit 1 file changed, 1 insertion(+) create mode 100644 hello.go
Pushing the code to a remote repository is left as an exercise for the reader.
Let‘s write a library and use it from the hello program.
Again, the first step is to choose a package path (we‘ll use github.com/user/newmath) and create the package directory:
$ mkdir $GOPATH/src/github.com/user/newmath
Next, create a file named sqrt.go in that directory with the following contents.
// Package newmath is a trivial example package. package newmath // Sqrt returns an approximation to the square root of x. func Sqrt(x float64) float64 { z := 1.0 for i := 0; i < 1000; i++ { z -= (z*z - x) / (2 * z) } return z }
Now, test that the package compiles with go build:
$ go build github.com/user/newmath
Or, if you are working in the package‘s source directory, just:
$ go build
This won‘t produce an output file. To do that, you must use go install, which places the package object inside the pkg directory of the workspace.
After confirming that the newmath package builds, modify your original hello.go (which is in $GOPATH/src/github.com/user/hello) to use it:
package main import ( "fmt" "github.com/user/newmath" ) func main() { fmt.Printf("Hello, world. Sqrt(2) = %v\n", newmath.Sqrt(2)) }
Whenever the go tool installs a package or binary, it also installs whatever dependencies it has. So when you install the hello program
$ go install github.com/user/hello
the newmath package will be installed as well, automatically.
Running the new version of the program, you should see some numerical output:
$ hello Hello, world. Sqrt(2) = 1.414213562373095
After the steps above, your workspace should look like this:
bin/ hello # command executable pkg/ linux_amd64/ # this will reflect your OS and architecture github.com/user/ newmath.a # package object src/ github.com/user/ hello/ hello.go # command source newmath/ sqrt.go # package source
Note that go install placed the newmath.a object in a directory inside pkg/linux_amd64 that mirrors its source directory. This is so that future invocations of the go tool can find the package object and avoid recompiling the package unnecessarily. The linux_amd64 part is there to aid in cross-compilation, and will reflect the operating system and architecture of your system.
Go command executables are statically linked; the package objects need not be present to run Go programs.
The first statement in a Go source file must be
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 newmath package by creating the file $GOPATH/src/github.com/user/newmath/sqrt_test.go containing the following Go code.
package newmath import "testing" func TestSqrt(t *testing.T) { const in, out = 4, 2 if x := Sqrt(in); x != out { t.Errorf("Sqrt(%v) = %v, want %v", in, x, out) } }
Then run the test with go test:
$ go test github.com/user/newmath ok github.com/user/newmath 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/newmath 0.165s
Run go help test and see the testing package documentation for more detail.
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 Mercurial repository hosted at Google Code,code.google.com/p/go.example. If you include the repository URL in the package‘s import path, go get will fetch, build, and install it automatically:
$ go get code.google.com/p/go.example/hello $ $GOPATH/bin/hello Hello, world. Sqrt(2) = 1.414213562373095
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:
bin/ hello # command executable pkg/ linux_amd64/ code.google.com/p/go.example/ newmath.a # package object github.com/user/ newmath.a # package object src/ code.google.com/p/go.example/ hello/ hello.go # command source newmath/ sqrt.go # package source sqrt_test.go # test source github.com/user/ hello/ hello.go # command source newmath/ sqrt.go # package source sqrt_test.go # test source
The hello command hosted at Google Code depends on the newmath 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 "code.google.com/p/go.example/newmath"
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.
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.
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.
How to Write Go Code,布布扣,bubuko.com
标签:des style http color os io for ar
原文地址:http://my.oschina.net/jerikc/blog/300962