gpt4 book ai didi

go - 我可以有像 os/arch 评论指令这样的自定义构建标志吗

转载 作者:IT王子 更新时间:2023-10-29 01:19:18 30 4
gpt4 key购买 nike

在 golang 中,我可以在文件的第一行注释中设置编译器指令,以确定该文件上的代码是否包含在基于 OS 或 arch 的构建中,例如

目标窗口:

// +build windows

或非windows:

// +build !windows

有没有办法在构建时传入我自己的 bool 变量以同样的方式运行?

背景是我想要一个 bool 调试标志,我可以传入它来进行调试构建,我不希望我的调试代码包含在正常构建中。

我目前正在做这样的事情:

go build -ldflags "-X main.Debug=true"

但我更愿意使用第一行注释方法,特别是因为这种方法不会从构建中省略调试代码(我假设)。

理想情况下我想要:

debug-on.go

// +build debug

package debug

func Debug() bool {
return true
}

debug-off.go

// +build !debug

package debug

func Debug() bool {
return false
}

更新 RE 副本我接受相关问题具有相同的主题,但它实际上不是重复的问题,这是来自已经知道此功能但正在努力实现的人的问题。

我的 2 美分是它似乎等同于“法国人说什么语言?” vs "'这不是重复的'用法语怎么说?"

最佳答案

Command go

Go is a tool for managing Go source code.

Usage:

go command [arguments]

Compile packages and dependencies

Usage:

go build [-o output] [-i] [build flags] [packages]

Build compiles the packages named by the import paths, along with their dependencies, but it does not install the results.

The build flags are shared by the build, clean, get, install, list, run, and test commands:

-tags 'tag list'
a space-separated list of build tags to consider satisfied during the
build. For more information about build tags, see the description of
build constraints in the documentation for the `go/build` package.

go/build package: Build Constraints


例如,在源码中,

// +build debug

在运行时,对于buildcleangetinstalllistruntest go 命令,

$ go install -tags='debug'

关于go - 我可以有像 os/arch 评论指令这样的自定义构建标志吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47253852/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com