6.2 KiB
#1.3 Go commands
Go commands
The Go language comes with a complete set of command operation tools, you can execute the command line go to see them:
Figure 1.3 Go command displays detailed information
These are all useful for us. Let's see how to use some of them.
go build
This command is for compiling tests. It will compile dependence packages if it's necessary.
- If the package is not the
mainpackage such asmymathin section 1.2, nothing will be generated after you executedgo build. If you need package file.ain$GOPATH/pkg, usego installinstead. - If the package is the
mainpackage, it will generate an executable file in the same folder. If you want the file to be generated in$GOPATH/bin, usego installorgo build -o ${PATH_HERE}/a.exe. - If there are many files in the folder, but you just want to compile one of them, you should append file name after
go build. For example,go build a.go.go buildwill compile all the files in the folder. - You can also assign the name of file that will be generated. For instance, we have
mathappin section 1.2, usego build -o astaxie.exewill generateastaxie.exeinstead ofmathapp.exe. The default name is your folder name(non-main package) or the first source file name(main package).
(According to The Go Programming Language Specification, package name should be the name after the word package in the first line of your source files, it doesn't have to be the same as folder's, and the executable file name will be your folder name as default.])
-
go buildignores files whose name starts with_or.. -
If you want to have different source files for every operating system, you can name files with system name as suffix. Suppose there are some source files for loading arrays, they could be named as follows.
array_linux.go | array_darwin.go | array_windows.go | array_freebsd.go
go build chooses the one that associated with your operating system. For example, it only compiles array_linux.go in Linux systems, and ignores all the others.
go clean
This command is for cleaning files that are generated by compilers, including the following files.
_obj/ // old directory of object, left by Makefiles
_test/ // old directory of test, left by Makefiles
_testmain.go // old directory of gotest, left by Makefiles
test.out // old directory of test, left by Makefiles
build.out // old directory of test, left by Makefiles
*.[568ao] // object files, left by Makefiles
DIR(.exe) // generated by go build
DIR.test(.exe) // generated by go test -c
MAINFILE(.exe) // generated by go build MAINFILE.go
I usually use this command to clean my files before I upload my project to the Github. These are useful for local tests, but useless for version control.
go fmt
The people who are working with C/C++ should know that people are always arguing about code style between K&R-style and ANSI-style, which one is better. However in Go, there is only one code style which is enforced. For example, you must put a left brace in the end of the line, and can't put it in a single line, otherwise you will get compile errors! Fortunately, you don't have to remember these rules. go fmt does this job for you. Just execute the command go fmt <File name>.go in terminal. I don't use this command very much because IDEs usually execute this command automatically when you save source files. I will talk about IDEs more in next section.
We usually use gofmt -w instead of go fmt, the latter will not rewrite your source files after formatted code. gofmt -w src formats the whole project.
go get
This command is for getting remote packages. It supports BitBucket, Github, Google Code and Launchpad so far. There are actually two things that happen after we execute this command. The first thing is to download the source code, then execute go install. Before you use this command, make sure you have installed all the related tools.
BitBucket (Mercurial Git)
Github (git)
Google Code (Git, Mercurial, Subversion)
Launchpad (Bazaar)
In order to use this command, you have to install these tools correctly. Don't forget to set PATH. By the way, it also supports customized domain names, use go help remote for more details.
go install
This command compiles all packages and generates files, then moves them to $GOPATH/pkg or $GOPATH/bin.
go test
This command loads all files whose name include *_test.go and generates test files, then prints information that looks like the following.
ok archive/tar 0.011s
FAIL archive/zip 0.022s
ok compress/gzip 0.033s
...
It tests all your test files by default, use command go help testflag for more details.
go doc
Many people said that we don't need any third-party documentation for programming in Go (actually I've made a CHM already), Go has a powerful tool to manage documentation by itself.
So how do we look up package information in documentation? If you want to get more details about the package builtin, use command go doc builtin, and use command go doc net/http for package http. If you want to see more details about specific functions, use command godoc fmt Printf, and godoc -src fmt Printf to view source code.
Execute command godoc -http=:8080, then open 127.0.0.1:8080 in your browsers, you should see a localized golang.org. It can not only show the standard packages' information, but also packages in your $GOPATH/pkg. It's great for people who are suffering from the Great Firewall of China.
Other commands
Go provides more commands then I just talked about.
go fix // upgrade code from old version before go1 to new version after go1
go version // get information about Go version
go env // view environment variables about Go
go list // list all installed packages
go run // compile temporary files and run the application
There are also more details about commands that I talked about, you can use go help <command> to get more information.
Links
- Directory
- Previous section: $GOPATH and workspace
- Next section: Go development tools
