以下命令基于go版本1.15.3进行测试
基本命令
go help
go env
go version
交叉编译
# Windows下编译Linux
SET CGO_ENABLED=0
SET GOARCH=amd64
SET GOOS=linux
go build xx.go
编译参数
go build
- 当编译包时,会自动忽略'_test.go'的测试文件。
# go build -o 指定编译输出的名称
# win下编译生成指定名称的可执行文件
go build -o custom.exe send.go
gofmt 和 go fmt
go fmt 是 gofmt的简易封装
go fmt 即 gofmt -l -w
传入文件的话,会格式化这个文件。传入目录的话,会格式化该目录下的所有.go文件
// 常用go fmt
go fmt send.go
// gofmt 参数
usage: gofmt [flags] [path ...]
-cpuprofile string
write cpu profile to this file
-d display diffs instead of rewriting files
-e report all errors (not just the first 10 on different lines)
-l list files whose formatting differs from gofmt's
-r string
rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')
-s simplify code
-w write result to (source) file instead of stdout
// go fmt 参数
usage: go fmt [-n] [-x] [packages]
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.
To run gofmt with specific options, run gofmt itself.
See also: go fix, go vet.
包管理 go mod
命令 | 说明 |
---|---|
download | download modules to local cache(下载依赖包) |
edit | edit go.mod from tools or scripts(编辑go.mod) |
graph | print module requirement graph (打印模块依赖图) |
init | initialize new module in current directory(在当前目录初始化mod) |
tidy | add missing and remove unused modules(拉取缺少的模块,移除不用的模块) |
vendor | make vendored copy of dependencies(将依赖复制到vendor下) |
verify | verify dependencies have expected content (验证依赖是否正确) |
why | explain why packages or modules are needed(解释为什么需要依赖) |
go help mod
go mod init xx (初始化)
go mod tidy (自动更新依赖)
有疑问加站长微信联系(非本文作者)