Golang构建约束随机

huangapple go评论110阅读模式
英文:

Golang Build Constraints Random

问题

我有两个带有不同构建约束的go文件。

constants_production.go:

  1. // +build production,!staging
  2. package main
  3. const (
  4. URL = "production"
  5. )

constants_staging.go:

  1. // +build staging,!production
  2. package main
  3. const (
  4. URL = "staging"
  5. )

main.go:

  1. package main
  2. func main() {
  3. fmt.Println(URL)
  4. }

当我执行go install -tags "staging"时,有时会打印production,有时会打印staging。同样,当我执行go install -tags "production"时,...

如何在每次构建时获得一致的输出?当我指定staging作为构建标志时,如何使其打印staging?当我指定production作为构建标志时,如何使其打印production?我在这里做错了什么吗?

英文:

I have two go files with different build constraints in the header.

constants_production.go:

  1. // +build production,!staging
  2. package main
  3. const (
  4. URL = "production"
  5. )

constants_staging.go:

  1. // +build staging,!production
  2. package main
  3. const (
  4. URL = "staging"
  5. )

main.go:

  1. package main
  2. func main() {
  3. fmt.Println(URL)
  4. }

When I do a go install -tags "staging", sometimes, it prints production; Sometimes, it prints staging. Similarly, when I do go install -tags "production",...

How do I get a consistent output on every build? How do I make it print staging when I specify staging as a build flag? How do I make it print production when I specify production as a build flag? Am I doing something wrong here?

答案1

得分: 7

go buildgo install在看起来没有任何更改的情况下不会重新构建包(二进制文件),并且不会对命令行构建标签的更改敏感。

一种查看此情况的方法是添加-v以打印构建的包:

  1. $ go install -v -tags "staging"
  2. my/server
  3. $ go install -v -tags "production"
  4. (没有输出)

您可以通过添加-a标志来强制重新构建,但这可能有点过度:

  1. $ go install -a -v -tags "production"
  2. my/server

...或者在构建之前触摸服务器源文件:

  1. $ touch main.go
  2. $ go install -a -tags "staging"

...或者在构建之前手动删除二进制文件:

  1. $ rm .../bin/server
  2. $ go install -a -tags "production"
英文:

go build and go install will not rebuild the package (binary) if it looks like nothing has changed -- and it's not sensitive to changes in command-line build tags.

One way to see this is to add -v to print the packages as they are built:

  1. $ go install -v -tags "staging"
  2. my/server
  3. $ go install -v -tags "production"
  4. (no output)

You can force a rebuild by adding the -a flag, which tends to be overkill:

  1. $ go install -a -v -tags "production"
  2. my/server

...or by touching a server source file before the build:

  1. $ touch main.go
  2. $ go install -a -tags "staging"

...or manually remove the binary before the build:

  1. $ rm .../bin/server
  2. $ go install -a -tags "production"

huangapple
  • 本文由 发表于 2014年10月14日 02:10:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/26346261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定