用户登录
用户注册

分享至

Go语言中一些不常见的命令参数详解

  • 作者: 临汾湿干家
  • 来源: 51数据库
  • 2021-10-09

前言

这篇文章可能会有些偏见。这篇文章描述了个人会用到的go工具参数,还有一些是我周围的人遇到的问题。如果有问题大家可以留言,你是刚开始使用go工具么?或者你想扩展知识?这篇文章将会描述每个人都需要知道的go工具参数。下面话不多说了,来一看看详细的介绍吧。

$ go build -x

-x会列出来go build调用到的所有命令。

如果你对go的工具链好奇,或者使用了一个跨c编译器,并且想知道调用外部编译器用到的具体参数,或者怀疑链接器有bug;使用-x来查看所有调用。

$ go build -x
work=/var/folders/00/1b8h8000h01000cxqpysvccm005d21/t/go-build600909754
mkdir -p $work/hello/perf/_obj/
mkdir -p $work/hello/perf/_obj/exe/
cd /users/jbd/src/hello/perf
/users/jbd/go/pkg/tool/darwin_amd64/compile -o $work/hello/perf.a -trimpath $work -p main -complete -buildid bbf8e880e7dd4114f42a7f57717f9ea5cc1dd18d -d _/users/jbd/src/hello/perf -i $work -pack ./perf.go
cd .
/users/jbd/go/pkg/tool/darwin_amd64/link -o $work/hello/perf/_obj/exe/a.out -l $work -extld=clang -buildmode=exe -buildid=bbf8e880e7dd4114f42a7f57717f9ea5cc1dd18d $work/hello/perf.a
mv $work/hello/perf/_obj/exe/a.out perf
 
$ go build -gcflags

这个参数将会传递给编译器。go tool compile -help列出来了所有我们可以传递给编译器的参数。

例如,禁用编译器优化和内联优化,你可以使用下面的参数:

$ go build -gcflags="-n -i"

$ go test -v

这个命令可以为测试提供完整的输出。它会打印测试名称、状态(成功或者失败)、测试所耗费的时间,还有测试的日志等等。

如果不使用-v参数来测试,输出很少很多,我经常使用-v参数来打开详细测试日志。例子:

$ go test -v context
=== run testbackground
--- pass: testbackground (0.00s)
=== run testtodo
--- pass: testtodo (0.00s)
=== run testwithcancel
--- pass: testwithcancel (0.10s)
=== run testparentfinisheschild
--- pass: testparentfinisheschild (0.00s)
=== run testchildfinishesfirst
--- pass: testchildfinishesfirst (0.00s)
=== run testdeadline
--- pass: testdeadline (0.16s)
=== run testtimeout
--- pass: testtimeout (0.16s)
=== run testcanceledtimeout
--- pass: testcanceledtimeout (0.10s)
...
pass
ok context 2.426s

$ go test -race

现在可以使用go工具提供的-race参数进行竞争检测。它会检测并报告竞争。开发的过程中用这个命令来检测一下。

注:完整的命令是:

$ go test -race mypkg // to test the package
$ go run -race mysrc.go // to run the source file
$ go build -race mycmd // to build the command

$ go test -run

你可以在测试的时候通过-run参数来正则匹配过滤需要测试的代码。下面的命令只会运行test examples。

$ go test -run=example

$ go test -coverprofile

当测试一个包的时候,可以输出一个测试覆盖率,然后使用命令go tool来在浏览器里面可视化。

$ go test -coverprofile=c.out && go tool cover -html=c.out

注:测试fmt包

go test -coverprofile=c.out fmt

$ go test -exec

一般很少有人知道go的这个功能,你可以通过-exec插入另一个程序。这个参数允许通过go工具完成一些外部工作。

一个常见的需求场景是你需要在一些宿主机上面执行一些测试。我们可以通过-exec命令调用adb命令来把二进制文件导入安卓设备并且可以收集到结果信息。参考这个来在安卓设备上面执行。

$ go get -u

如果你通过go get命令获取go包,而这个包已经存在于本地的gopath,那么这个命令并不会帮你更新包。-u可以强制更新到最新版。

如果你是一个库作者,你最好在你的安装说明上加上-u参数,例如,是这么做的:

$ go get -u github.com/golang/lint/golint

$ go get -d

如果你想clone一个代码仓库到gopath里面,跳过编译和安装环节,使用-d参数。这样它只会下载包并且在编译和安装之前停止。

当需要clone虚拟网址代码仓库的时候,我经常使用这个命令来代替git clone,因为这样可以把go代码自动放入合适的目录下面。例如:

$ go get -d golang.org/x/oauth2/...

这样可以克隆到$gopath/src/golang.org/x/ouath2目录下面。假设golang.org/x/oauth2是一个虚拟网址,通过go get获取这个代码仓库要比找出仓库的真实地址(go.googlesource.com/oauth2)更简单。

$ go get -t

如果你的测试包的有附加的依赖包,-t可以一并下载测试包的依赖包。如果没有加这个参数,go get只会下载非测试包的依赖包。

$ go list -f

这个命令可以列出来go的所有包,并且可以指定格式。这个写脚本的时候很有用。

下面这个命令将会打印所有依赖的runtime包

go list -f ‘' runtime [runtime/internal/atomic runtime/internal/sys unsafe]

参考链接:

http://blog.csdn.net/erlib/article/details/52703165

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

软件
前端设计
程序设计
Java相关