build - Golang 构建约束随机

标签 build compilation go

我有两个在 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 buildgo 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/

相关文章:

go - 如何实现链表

google-app-engine - 找不到包 "appengine/cloudsql"

c++ - C++ Primer 中的类进入循环依赖

windows - 我可以将 Gradle 和 Maven 设置为在 Windows 中的 %userprofile% 之外的其他目录中安装它们的本地存储库吗?

c# - 如何手动编辑 Visual Studio 项目的 list 以明确指定引用的程序集?

c++ - 在这种特定情况下我应该使用头文件吗?

android - 从命令行编译Android项目很慢

go - 如何检查函数参数和类型

qt - 即使我设置了 CMAKE_PREFIX_PATH,Cmake 似乎也找不到所需的 Qt cmake 文件

android gradle 本地路径不存在