Go commands
Last updated
Was this helpful?
Last updated
Was this helpful?
The Go language comes with a complete set of command operation tools. You can execute the go
command on the terminal to see them:
These are all useful for us. Let's see how to use some of them.
This command is for compiling tests. It will compile packages and dependencies if it's necessary.
If the package is not the main
package such as mymath
in section 1.2, nothing will be generated after you execute go build
. If you need the package file .a
in $GOPATH/pkg
, use go 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
, use go install
or go 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 the 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 the file that will be generated. For instance, in the mathapp
project (in section 1.2), using go build -o astaxie.exe
will generate astaxie.exe
instead of mathapp.exe
. The default name is your folder name (non-main package) or the first source file name (main package).
go build
ignores files whose names start with _
or .
.
If you want to have different source files for every operating system, you can name files with the system name as a 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's 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 cleaning files that are generated by compilers, including the following files:
I usually use this command to clean up my files before I upload my project to 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 which code style is better: K&R-style or ANSI-style. However in Go, there is only one code style which is enforced. For example, left braces must only be inserted at the end of lines, and they cannot be on their own lines, 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 more about IDEs in the next section.
go fmt
is just an alias, which runs the command gofmt -l -w
on the packages named by the import paths.
We usually use gofmt -w
instead of go fmt
. The latter will not rewrite your source files after formatting code. gofmt -w src
formats the whole project.
This command is for getting remote packages. So far, it supports BitBucket, GitHub, Google Code and Launchpad. There are actually two things that happen after we execute this command. The first thing is that Go downloads the source code, then executes go install
. Before you use this command, make sure you have installed all of the related tools.
In order to use this command, you have to install these tools correctly. Don't forget to update the $PATH
variable. By the way, it also supports customized domain names. Use go help importpath
for more details about this.
This command compiles all packages and generates files, then moves them to $GOPATH/pkg
or $GOPATH/bin
.
This command loads all files whose name include *_test.go
and generates test files, then prints information that looks like the following.
It tests all your test files by default. Use command go help testflag
for more details.
So how do we look up package information in documentation? For instance, if you want to get more details about the builtin
package, use the godoc builtin
command. Similarly, use the godoc net/http
command to look up the http
package documentation. If you want to see more details about specific functions, use the godoc fmt Printf
and godoc -src fmt Printf
commands to view the source code.
Execute the godoc -http=:8080
command, then open 127.0.0.1:8080
in your browser. 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 than those we've just talked about.
There are also more details about the commands that I've talked about. You can use go help <command>
to look them up.
(According to , package names 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 the folder name, and the executable file name will be your folder name by default.)
Many people say that we don't need any third-party documentation for programming in Go (actually I've made a already). Go has a powerful tool to manage documentation natively.
Previous section:
Next section: