Golang构建约束随机

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

Golang Build Constraints Random

问题

我有两个带有不同构建约束的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"时,...

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

英文:

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

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)
}

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以打印构建的包:

$ go install -v -tags "staging"
my/server
$ go install -v -tags "production"
(没有输出)

您可以通过添加-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"
英文:

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:

$ go install -v -tags "staging"
my/server
$ go install -v -tags "production"
(no output)

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

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

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

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

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

$ rm .../bin/server
$ 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:

确定