View available commands
Help go directly input in the terminal to display all the go command with the appropriate command function profile, these are mainly the following:
build: 编译包和依赖
clean: 移除对象文件
doc: 显示包或者符号的文档
env: 打印go的环境信息
bug: 启动错误报告
fix: 运行go tool fix
fmt: 运行gofmt进行格式化
generate: 从processing source生成go文件
get: 下载并安装包和依赖
install: 编译并安装包和依赖
list: 列出包
run: 编译并运行go程序
test: 运行测试
tool: 运行go提供的工具
version: 显示go的版本
vet: 运行go tool vet
Use command is: go command [args]
in addition, can be used go help <command>
to display additional help for the specified command.
When you run go help, not only print the basic information on these commands, also gives some information to help concepts:
c: Go和c的相互调用
buildmode: 构建模式的描述
filetype: 文件类型
gopath: GOPATH环境变量
environment: 环境变量
importpath: 导入路径语法
packages: 包列表的描述
testflag: 测试符号描述
testfunc: 测试函数描述
Also use go help <topic>
to view information on these concepts.
build
And run
command
Like other statically typed languages, go to execute the program, you need to compile and then execute the executable file generated. go build command is used to compile the go program generates executable file. But not so the program can go compiled executable file to generate an executable file, go program needs to meet two conditions:
that the program needs to go belongs to the main package
must have the main function included in the main package
that is the inlet is main.main go procedure, i.e., the main bag main function examples (hello.go):
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
Compile hello.go, and then run the executable:
$ go run hello.go # 将会生成可执行文件 hello
$ ./hello # 运行可执行文件
Hello World!
The above is the go build
basic usage, if you use another go build compiled is not an executable program, but a package, it will not generate an executable file.
And go run command can be executed in one step and two steps above (no intermediate file).
$ go run hello.go
Hello World!
These two commands are very commonly used in development.
Furthermore go clean
commands can be used to produce the executable program will clear:
$ go clean # 不加参数,可以删除当前目录下的所有可执行文件
$ go clean sourcefile.go # 会删除对应的可执行文件
fmt
And doc
command
go have a mixed language of nature, it is very strict requirements for the format, I really like this feature because you can keep the code clear and consistent, compile a combination of development and go also it provides a very powerful tool to format code, it is to go fmt sourcefile.go, but usually we actually do not need to manually call, various editors can help us automate formatting.
go doc command can help us quickly see the package documentation, go doc package command will print out the document specified package in the terminal.
In addition there is a command related to go doc commands are godoc, we can start the server by its own documents:
godoc -http=:8080
Then we can with a browser localhost:8080
to view the document go in
get
command
This command is also very common, we can use it to download and install third-party packages, use:
go get src
Download the update from a specified source or above specified code and dependencies, and they were compiled and installed, for example, we want to use beego to develop web applications, we first need to obtain beego:
go get github.com/astaxie/beego
This command will automatically download and install beego and its dependencies, and then we can use the following ways:
package main
import "github.com/astaxie/beego" # 这里需要使用 src 下的完整路径
func main() {
beego.Run()
}
install
Command
used to compile and install the program go, we can compare it with the build command:
install build
The current directory under the bin directory under the working directory path to the executable file generated
Source directory name with the same name as the executable file and the default source of the same name, you can use the -o
option to specify
Dependence will depend on the package into the working directory pkg
folder
test
command
As the name suggests, the command used to run the test, this test is based package units. You need to follow certain rules:
Testing is the name of the source file with _test.go
suffix
Test source file containing source files of a number of test functions
Test function generally based on Test
the name of the prefix, and there is a type *testing.T
of argument.
Other commands
Other commands do not often use, not presented here, when really used, directly go help command to view the command.