Golang Build Constraints Random

我们两清 提交于 2021-02-07 20:36:14

问题


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:


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"


来源:https://stackoverflow.com/questions/26346261/golang-build-constraints-random

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!