go build flags 解析

1 篇文章 0 订阅
1 篇文章 0 订阅
  1. build flags 使用于以下命令:go build, go clean, go get, go install, go list,go run, go test 如:go build -n -a .
  2. 可用参数:
        -a
               强制重新构建涉及的所有包。
        -n
                打印将要执行的命令,但不执行
        -p n
                构建时并行数(n).
                默认`GOMAXPROCS`
        -race
                开启数据竞争检测。
                支持的平台: linux/amd64, freebsd/amd64, darwin/amd64, darwin/arm64, windows/amd64,
                linux/ppc64le and linux/arm64 (only for 48-bit VMA).
        -msan
                enable interoperation with memory sanitizer.
                Supported only on linux/amd64, linux/arm64
                and only with Clang/LLVM as the host C compiler.
                On linux/arm64, pie build mode will be used.
        -asan
                enable interoperation with address sanitizer.
                Supported only on linux/arm64, linux/amd64.
        -v
                打印构建的包名称
        -work
                打印构建临时工作目录并保留
        -x
                打印构建期间使用的命令

        -asmflags '[pattern=]arg list'
                构建过程中调用`go tool asm`时使用的参数。参数描述见:`go tool asm`
        -buildmode mode
               设置构建模式,如插件模式:`-buildmode=plugin`。参数描述见:`go help buildmode`
        -buildvcs
                是否用版本控制信息标记二进制文件 ("true", "false", 或 "auto"). 默认 ("auto"), 如果main包、main模块包含的目录、当前目录都在同一个存储库(git/mercurial/fossil/bazaar等)中那么版本控制信息会标记到二进制文件中。
                `-buildvcs=false` 忽略版本控制信息。
                `-buildvcs=true` 版本控制信息可用但因缺少工具或文件结构不明确导致无法将信息标记到二进制文件中将会报错。
                另:版本控制信息和构建信息都可以使用`go version -m go编译的二进制文件``runtime/debug.ReadBuildInfo()``debug/buildinfo.Read()`获取
        -compiler name
                构建时使用的编译器名称,  `runtime.Compiler `(gccgo 或 gc).
        -gccgoflags '[pattern=]arg list'
                设置gccgo编译器的参数
        -gcflags '[pattern=]arg list'
                构建过程中调用`go tool compile`时使用的参数。参数描述见:`go tool compile`
        -installsuffix suffix
                a suffix to use in the name of the package installation directory,
                in order to keep output separate from default builds.
                If using the -race flag, the install suffix is automatically set to race
                or, if set explicitly, has _race appended to it. Likewise for the -msan
                and -asan flags. Using a -buildmode option that requires non-default compile
                flags has a similar effect.
        -ldflags '[pattern=]arg list'
                构建过程中调用`go tool link`时使用的参数。参数描述见:`go tool link`
                另:`go build -ldflags="-X 'main.buildTime=`date`'" .`可以将构建时间打入变量`main.buidTime``buidTime`必须为string类型,是否可导出无所谓)中。
        -linkshared
                build code that will be linked against shared libraries previously
                created with -buildmode=shared.
        -mod mode
                模块加载方式:readonly, vendor, 或 mod。
                参考:https://go.dev/ref/mod#build-commands
                leave newly-created directories in the module cache read-write
                instead of making them read-only.
        -modfile file
                in module aware mode, read (and possibly write) an alternate go.mod
                file instead of the one in the module root directory. A file named
                "go.mod" must still be present in order to determine the module root
                directory, but it is not accessed. When -modfile is specified, an
                alternate go.sum file is also used: its path is derived from the
                -modfile flag by trimming the ".mod" extension and appending ".sum".
        -overlay file
                read a JSON config file that provides an overlay for build operations.
                The file is a JSON struct with a single field, named 'Replace', that
                maps each disk file path (a string) to its backing file path, so that
                a build will run as if the disk file path exists with the contents
                given by the backing file paths, or as if the disk file path does not
                exist if its backing file path is empty. Support for the -overlay flag
                has some limitations: importantly, cgo files included from outside the
                include path must be in the same directory as the Go package they are
                included from, and overlays will not appear when binaries and tests are
                run through go run and go test respectively.
        -pkgdir dir
                install and load all packages from dir instead of the usual locations.
                For example, when building with a non-standard configuration,
                use -pkgdir to keep generated packages in a separate location.
        -tags tag,list
                以逗号分隔的tag列表控制编译条件,配合`//go:build tag`使用。参考:https://pkg.go.dev/cmd/go#hdr-Build_constraints
        -trimpath
                从生成的可执行文件中移除所有的文件系统路径。
                记录文件名时使用`module path@version`或普通导入路径。
                不会暴露主机的路径信息,这在日志输出代码位置时非常有用。
        -toolexec 'cmd args'
                a program to use to invoke toolchain programs like vet and asm.
                For example, instead of running asm, the go command will run
                'cmd args /path/to/asm <arguments for asm>'.
                The TOOLEXEC_IMPORTPATH environment variable will be set,
                matching 'go list -f {{.ImportPath}}' for the package being built.


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值