Go 命令

含义

go build

如果是普通包,就像我们在第1.2节中编写的mymath包那样,当你执行go build之后,它不会产生任何文件。如果你需要在$GOPATH/pkg 下生成相应的文件,则要执行go install。

如果是main包,当你执行go build之后,它就会在当前目录下生成一个可执行文件。如果你需要在$GOPATH/bin下生成相应的文件,需要执行go install,或者使用go build -o路径/a.exe。

如果某个项目文件夹下有多个文件,而你只想编译某个文件,就可在go build之后加上文件名,例如go build a.go;go build命令默认会编译当前目录下的所有go文件。

你也可以指定编译输出的文件名。例如第1.2节中的mathapp应用,我们可以指定gobuild -o astaxie.exe,默认情况是你的package名(非main包),或者是第一个源文件的文件名(main包)。

go build会忽略目录下以“_”或“.”开头的go文件。

如果你的源代码针对不同的操作系统需要不同的处理,那么你可以根据不同的操作系统后缀来命名文件。例如,有一个读取数组的程序,它对于不同的操作系统可能有如下几个源文件:array_linux.go array_darwin.go array_windows.go array_freebsd.go。使用go build的时候会选择性地编译以系统名结尾的文件(Linux、darwin、Windows、freebsd)。例如,Linux系统下面编译只会选择array_linux.go文件,其他系统命名后缀文件全部忽略。

出处:​​https://weread.qq.com/web/reader/cef329105a640acef144d1ck45c322601945c48cce2e120​

go clean

出处:​​https://weread.qq.com/web/reader/cef329105a640acef144d1ck45c322601945c48cce2e120​

go doc

如何查看相应的package文档呢?如果是builtin包,那么执行go doc builtin;如果是http包,那么执行go doc net/http;查看某一个包里面的函数,则执行godoc fmt Printf;也可以查看相应的代码,执行godoc -src fmt Printf。

通过命令行的方式执行 godoc -http=:端口号,比如godoc -http=:8080。然后在浏览器中打开127.0.0.1:8080,你将会看到一个golang.org的本地副本,通过它可查询pkg文档等其他内容。如果你设置了 GOPATH,在 pkg 分类下,不但会列出标准包的文档,还会列出本地 GOPATH中所有项目的相关文档,这对于经常被限制访问的用户来说是一个不错的选择。


go fmt

Go 语言工具集中提供了一个go fmt命令,它可以帮你格式化所写好的代码文件,使你在写代码的时候不需要关心格式,只需要在写完之后执行go fmt <文件名>.go,你的代码就被修改成了标准格式。

使用go fmt命令,更多的时候是用gofmt(go fmt使用gofmt工具来格式化,gofmt工具将代码以标准格式重写),而且需要参数-w,否则格式化结果不会写入文件。使用gofmt -w <文件夹>,可以格式化整个文件夹。

go install

这个命令在内部实际上分成两步操作:第一步是生成结果文件(可执行文件或者.a包),第二步会把编译好的结果移到$GOPATH/pkg或者$GOPATH/bin。

go get

这个命令用以动态获取远程代码包,目前支持的有BitBucket、GitHub、Google Code和Launchpad。这个命令在内部实际上分成两步操作:第一步是下载源码包,第二步是执行go install。

go get 将会从模块代码的 master 分支拉取,而若使用 Go Modules 则你可以利用 Git Tag 手动选择一个特定版本的模块代码;

go run

编译并运行Go语言程序。


go test

执行这个命令,会自动读取源码目录下名为*_test.go的文件,生成并运行测试用的可执行文件。

默认情况下,不需要任何参数,它会自动把你的源码包下面所有的test文件测试完毕,当然你也可以带上参数,详细内容请参考go help testflag。

$ go
Go is a tool for managing Go source code.

Usage:

go <command> [arguments]

The commands are:

bug start a bug report
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get add dependencies to current module and install them
install compile and install packages and dependencies
list list packages or modules
mod module maintenance
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet report likely mistakes in packages

Use "go help <command>" for more information about a command.

Additional help topics:

buildmode build modes
c calling between Go and C
cache build and test caching
environment environment variables
filetype file types
go.mod the go.mod file
gopath GOPATH environment variable
gopath-get legacy GOPATH go get
goproxy module proxy protocol
importpath import path syntax
modules modules, module versions, and more
module-get module-aware go get
module-auth module authentication using go.sum
module-private module configuration for non-public modules
packages package lists and patterns
testflag testing flags
testfunc testing functions

Use "go help <topic>" for more information about that topic.