我有两个在 header 中具有不同构建约束的 go 文件。
constants_production.go:
// +build production,!staging
package main
const (
URL = "production"
)
constants_staging.go:
// +build staging,!production
package main
const (
URL = "staging"
)
main.go:
package main
func main() {
fmt.Println(URL)
}
当我执行 go install -tags "staging"
时,有时会打印 production
;有时,它会打印 staging
。同样,当我执行 go install -tags "production"
时,...
如何在每次构建时获得一致的输出?当我将暂存指定为构建标志时,如何让它打印暂存?当我将生产指定为构建标志时,如何使其打印生产?我在这里做错了什么吗?
最佳答案
go build
和 go install
如果看起来没有任何变化,则不会重建包(二进制)——并且它对命令行构建中的变化不敏感标签。
查看此内容的一种方法是添加 -v
以在构建包时打印它们:
$ go install -v -tags "staging"
my/server
$ go install -v -tags "production"
(no output)
您可以通过添加 -a
标志来强制重建,这往往是矫枉过正的:
$ go install -a -v -tags "production"
my/server
...或者通过在构建之前访问服务器源文件:
$ touch main.go
$ go install -a -tags "staging"
...或在构建之前手动删除二进制文件:
$ rm .../bin/server
$ go install -a -tags "production"
关于build - Golang 构建约束随机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26346261/