如何在构建过程中使用不同的配置文件?

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

how to use different config files on build?

问题

我有一个用于我的Go程序的配置文件,在本地运行和在服务器上运行时,它的值明显不同。

我的老板最近要求我切换部署方法,即在本地运行"go build",然后部署该构建版本。因此,我需要每次构建时切换我的全局配置文件。

不幸的是,我记性很差,我几乎肯定会忘记切换这些文件。如果有一种方法(可能是在构建脚本文件中或其他任何地方)可以在构建时使用不同的配置文件,而不是像我在本地开发时通常做的那样运行"go run main.go",那就太好了。

有人有任何想法或最佳实践吗?

注意:我已经将这个文件添加到了gitignore中,但由于构建基本上将整个程序(包括这些设置变量)固化到一个单独的文件中,只有在服务器上有另一个文件是不够的。

谢谢大家!

英文:

I have a config file for my go program, and clearly it has different values when I run locally vs on my server.

My boss recently asked me to switch deployment methods so that I run "go build" locally, and then deploy that build. Thus, I need to switch out my global config file each time I build.

Unfortunately, I have a piss poor memory, and I'll almost certainly forget to switch these files out. I would love if there were way (maybe in a build script file, or anything) to use different config file when building as opposed to running "go run main.go" as I usually do when I am working locally on development.

Does anyone have any ideas or best practices here?

Note: I already gitignore this file, but since building if basically solidifies the whole program (including these set variables) into a single file, just having another file on the server wouldn't do.

Thanks all!

答案1

得分: 2

使用构建约束

以下是一个示例:

config.go

//go:build !live

// 在这里添加你的本地配置

config_live.go

//go:build live

// 在这里添加你的服务器配置

使用go build来构建本地版本。使用go build -tags live来构建服务器版本。

英文:

Use build constraints.

Here's an example:

config.go

//go:build !live

// Add your local config here

config_live.go

//go:build live

// Add your server config here

Use go build to build the local version. Use go build -tags live to build the server version.

huangapple
  • 本文由 发表于 2022年1月8日 11:59:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/70629506.html
匿名

发表评论

匿名网友

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

确定