#1.3 Go commands
Go language comes with a complete set of command operation tool, 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.
This command is for compiling tests, it will compile dependence packages if it's necessary.
- If the package is not the
main
package such asmymath
in section 1.2, nothing will be generated after you executedgo build
. If you need package file.a
in$GOPATH/pkg
, usego install
instead. - If the package is the
main
package, it will generate an executable file in the same folder. If you want the file to be generated in$GOPATH/bin
, usego install
orgo 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 build
will compile all the files in the folder. - You can also assign the name of file that will be generated. For instance, we have
mathapp
in section 1.2, usego build -o astaxie.exe
will generateastaxie.exe
instead 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 build
ignores 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.
This command is for clean files that are generated by compilers, including 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.
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 forced to use. For example, you must put 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 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.
This command is for getting remote packages, it supports BitBucket, Github, Google Code, Launchpad so far. There are actually two things happening after we executed this command. The first thing is to download source code, then executes go install
. Before you use this command, make sure you have installed 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.
This command compiles all packages and generate files, then move them to $GOPATH/pkg
or $GOPATH/bin
.
This command loads all files whose name include *_test.go
and generate test files, then prints information looks like follows.
ok archive/tar 0.011s
FAIL archive/zip 0.022s
ok compress/gzip 0.033s
...
It tests all your test files as default, use command go help testflag
for more details.
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 to look up packages' information in documentation? If you want to get more details about 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.
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.
- Directory
- Previous section: $GOPATH and workspace
- Next section: Go development tools