Go学习路径的Go命令模块,参考资料:Go帮助文档
在控制台使用Go命令可以打印出帮助文档,英文不好的同学可以看看我写的这个笔记,没啥水平我也在学,仅供参考,发现错误欢迎指正!
1.go bug
> Bug打开默认浏览器并启动新的错误报告。 > 该报告包含有用的系统信息。emm…这个命令我试了一下,并不是给你debug用的,而是你执行了go bug
后他会打开浏览器并跳转到go的github主页的issues页面,就是说如果你发现了go语言的bug,用这个命令就可以快速的通知go开发团队。orz…orz…
2.go build
>Build compiles the packages named by the import paths, >along with their dependencies, but it does not install the results.你可以对一堆包进行编译
If the arguments to build are a list of .go files, build treats
them as a list of source files specifying a single package.
你也可以编译一堆go文件
When compiling a single main package, build writes the resulting executable to an output file named after the first source file (‘go build ed.go rx.go’ writes ‘ed’ or ‘ed.exe’) or the source code directory (‘go build unix/sam’ writes ‘sam’ or ‘sam.exe’).The ‘.exe’ suffix is added when writing a Windows executable.
如果你只编译main包,最后可执行文件的名字就是这个包名,如果你编译一堆文件,最后可执行文件的名字就和第一个go文件同名
When compiling multiple packages or a single non-main package,build compiles the packages but discards the resulting object,serving only as a check that the packages can be built.
如果你编译多个包或者单个非main包,不会生成可执行文件
When compiling packages, build ignores files that end in ‘_test.go’.
如果你的go文件以_开头,编译时会忽略它
The -o flag, only allowed when compiling a single package, forces build to write the resulting executable or object to the named output file, instead of the default behavior described in the last two paragraphs.
如果你只编译一个包,可以用-o参数指定生成的可执行文件名
The -i flag installs the packages that are dependencies of the target.
-i参数安装目标依赖包
The build flags are shared by the build, clean, get, install, list, run, and test commands:
还有一些参数build clean get install list run test命令用法都一样,那这个就在文章最后写吧
这还有个我在网上看到的关于go build命令的操作,它可以编译成不同平台的可执行文件。比如说这个命令GOOS=linux GOARCH=amd64 go build ...
可以生成linux 64位程序。
GOOS | GOARCH |
---|---|
darwin | arm |
freebsd | arm64 |
linux | 386 |
windows | amd64 |
android | ppc64 |
dragonfly | ppc64le |
netbsd | mips64 |
openbsd | mips64le |
plan9 | s390x |
solaris |
3.go clean
>Clean removes object files from package source directories. >The go command builds most objects in a temporary directory,so go clean is mainly concerned with object files left by other tools or by manual invocations of go build.清理目录,只留源码,下面是参数列表
参数 | 含义 |
---|---|
- i | 删掉go install安装的 |
- n | 把要执行的删除命令打印出来,但不执行 |
- r | 删掉你导入的包 |
- x | 执行- n打印的那些命令 |
- cache | 删掉所有缓存 |
- testcache | 删掉test缓存 |
- modcache | 删掉模块下载缓存 |
4.go doc
> Doc prints the documentation comments associated with the item identified by itsarguments (a package, const, func, type, var, method, or struct field) followed by a one-line summary of each of the first-level items "under" that item (package-level declarations for a package, methods for a type,etc.).打印实体的文档注释,即package,const,func,type,var,method,struct这些东西。
Doc accepts zero, one, or two arguments. Given no arguments, that is, when run as
go doc
it prints the package documentation for the package in the current directory.
If the package is a command (package main), the exported symbols of the package
are elided from the presentation unless the -cmd flag is provided.
go doc命令可以接0 - 2个参数,如果不加参数会把目录中所有实体列出
下面的文档太长我自己总结一下:
一个参数可以指定你要的实体,跟在go doc命令后面,你就可以之查看它了。两个参数是第一个参数指定包,第二个参数指定你要看的实体
下面是一些参数:
参数 | 含义 |
---|---|
- all | 打印包的所有实体 |
- c | 区分大小写 |
- cmd | 打印main中实体,默认不打印 |
- src | 打印完整源码定义 |
- u | 打印非导出实体即首字母小写,默认不打印 |
5.go env
>Env prints Go environment information. > >By default env prints information as a shell script (on Windows, a batch file). If one or more variable names is given as arguments, env prints the value of each named variable on its own line.打印go环境的一些参数,你也可以在后面指定参数名
The -json flag prints the environment in JSON format instead of as a shell script.
也可以加- json参数以json格式打印
6.go fix
> Fix runs the Go fix command on the packages named by the import paths.如果go版本升级了,这个命令可以让你的代码也更新一下版本
7.go fmt
>Fmt runs the command 'gofmt -l -w' on the packages named by the import paths. It prints the names of the files that are modified. > >For more about gofmt, see 'go doc cmd/gofmt'. >For more about specifying packages, see 'go help packages'. > >The -n flag prints commands that would be executed. >The -x flag prints commands as they are executed.go fmt
和gofmt -l -w
一样用来格式化你的代码,下面是一些参数
参数 | 含义 |
---|---|
- n | 打印要执行的命令 |
- x | 打印被执行的命令 |
8.go generate
>Generate runs commands described by directives within existing files. Those commands can run any process but the intent is to create or update Go source files. > >Go generate is never run automatically by go build, go get, go test, and so on. It must be run explicitly. > >Go generate scans the file for directives, which are lines of the form, > > //go:generate command argument...你可以在你的go文件中加类似//go:generate ...
之类的注释,使用go generate
可以扫描到它们并执行它后面指令
Go generate sets several variables when it runs the generator:
使用go generate
有一些变量可以给我们用
变量 | 含义 |
---|---|
$GOARCH | 系统架构 |
$GOOS | 系统 |
$GOFILE | 文件名 |
$GOLINE | 当前命令在文件中的行号 |
$GOPACKAGE | 当前命令所在的包名 |
$DOLLAR | $标志 |
go generate
还有一些参数可以用
参数 | 含义 |
---|---|
- run | 正则表达式,执行匹配到的命令 |
- v | 打印被处理的包名和源文件名 |
- n | 打印命令但不执行 |
- x | 打印命令且执行 |
9.go get
>Get downloads the packages named by the import paths, along with their dependencies. It then installs the named packages, like 'go install'.下载包及其依赖,然后用go install
安装他们
参数 | 含义 |
---|---|
- d | 下载后不安装 |
- u | 更新包 |
- fix | 下载后做一下适配 |
- insecure | 允许不安全的下载 |
- t | 连测试包也下 |
- v | 启用详细进度和调试输出 |
10.go install
>Install compiles and installs the packages named by the import paths. > >The -i flag installs the dependencies of the named packages as well.安装指定的包,加 - i 参数可以安装其依赖项
11.go list
> List lists the named packages, one per line.The most commonly-used flags are -f and -json, which control the form of the output printed for each package.go list
命令输出指定包信息,最常用的是 - f,- json 参数
加上json可以json格式显示完整信息,-f可以显示指定信息
12.go mod
不会...看不懂...建议百度...13.go run
编译并执行go文件,与`go build`一样,但它不留存可执行文件14.go test
>'Go test' recompiles each package along with any files with names matching the file pattern "*_test.go". >These additional files can contain test functions, benchmark functions, and example functions. See 'go help testfunc' for more. >Each listed package causes the execution of a separate test binary. >Files whose names begin with "_" (including "_test.go") or "." are ignored._ > >Test files that declare a package with the suffix "_test" will be compiled as a separate package, and then linked and run with the main test binary. > >The go tool will ignore a directory named "testdata", making it available to hold ancillary data needed by the tests.执行go test
会编译运行那些xxx_test.go
文件,而这些文件在正常编译时会被忽略,且以testdata
命名的目录也会被忽略,使其可用于保存测试所需的辅助数据
15.go tool
>Tool runs the go tool command identified by the arguments. >With no arguments it prints the list of known tools. > >The -n flag causes tool to print the command that would be executed but not execute it.go tool
命令打印你现在有什么工具,-n参数使工具打印将要执行但不执行它的命令。
16.go version
> Version prints the Go version, as reported by runtime.Version.这个很简单233,就是go version
打印你的go语言版本
17.go vet
`go vet`是一个用于检查Go语言源码中静态错误的简单工具支持-n打印不执行与-x打印并执行两个参数
最后是build, clean, get, install, list, run, test这些命令通用的参数表
参数 | 含义 |
---|---|
-a | 强制重建已经是最新的软件包。 |
-n | 打印命令但不执行 |
-p n | 可以并行运行的程序数 |
-race | 启用数据竞争检测仅支持64位 |
-msan | 启用内存清理程序互操作,仅在linux |
-v | 编译时打印包名 |
-work | 打印临时目录名,且退出时不删除 |
-x | 打印执行的命令 |