Home > Back-end >  how to use different config files on build?
how to use different config files on build?

Time:01-08

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!

CodePudding user response:

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.

  •  Tags:  
  • Related