Build Golang构建约束随机

Build Golang构建约束随机,build,compilation,go,Build,Compilation,Go,我有两个go文件,在标题中有不同的构建约束 常数_production.go: // +build production,!staging package main const ( URL = "production" ) 常量_staging.go: // +build staging,!production package main const ( URL = "staging" ) main.go: package

我有两个go文件,在标题中有不同的构建约束

常数_production.go:

// +build production,!staging

package main

const (
  URL               = "production"
)
常量_staging.go:

// +build staging,!production

package main

const (
  URL               = "staging"
)
main.go:

package main

func main() {
  fmt.Println(URL)
}
当我进行
go安装时-标记“staging”
,有时,它会打印
生产
;有时,它会打印
staging
。类似地,当我安装时,标记“生产”


如何在每个构建中获得一致的输出?当我将暂存指定为生成标志时,如何使其打印暂存?当我将生产指定为构建标志时,如何使其打印生产?我在这里做错了什么吗?

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"
…或在生成之前手动删除二进制文件:

$ touch main.go
$ go install -a -tags "staging"
$ rm .../bin/server
$ go install -a -tags "production"

是的,这就是问题所在。二进制文件没有像你说的那样被重建。谢谢