#1.3 Go commands
##Go commands
Go has a integral command system, you can simply type go
in terminal to see what are the commands that you can use.
Figure 1.3 Details of Go commands in terminal
These are all useful for us, let's see how to use them.
##go build 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
, you should usego install
instead. - If the package is the
main
package, it will generate a 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 file, you should add file name after
go build
. For example,go build a.go
.go build
will compile all the files in 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 same as folder's, and the executable file name will be your folder name as default.])
-
go build
will ignore files whose names begin with_
or.
. -
If you want to have different source code 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 following.
array_linux.go | array_darwin.go | array_windows.go | array_freebsd.go
go build
will choose the one associated with your operating system. For example, it will only compile array_linux.go in Linux systems, and ignore all the others.
##go clean This command is for clean files which 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.
##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 style which is forced to use, like 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 by yourself, go fmt
will do 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.
Usually we use gofmt -w
instead of go fmt
, the latter will not rewrite your source files after formated. gofmt -w src
will format the whole project.
##go get
This command is for getting remote packages, it supports bitbucket, github, googlecode, launchpad so far. There are actually two things happening after we executed this command. The first is to download source code, and then executes go install
. Before you use this command, make sure you have installed related.
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 will compile all packages and generate files, then move them to $GOPATH/pkg
or $GOPATH/bin
.
##go test
This command will load all files whose name include *_test.go
and generate test files, and print information looks like following.
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.
##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 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 will 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 fucking
#GFW
##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 isntalled packages
go run // compile temporary files and run the application
There are also more details about commands 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