How to work with multiple environment like dev, test, staging, prod in golang ?

166 views
Skip to first unread message

Shishira Pradhan

unread,
May 17, 2020, 1:19:23 PM5/17/20
to golang-nuts
Hi All,

i'm a java developer, currently working on golang. In springboot, we have configuration like port, database etc info are stored in yaml files like application-dev.yml,   application-test.yml, application-prod.yml profiles, and profile name is passed during running the application to load profile specific configuration. I need to understand that how to load/pass profile based properties in golang ?

Thanks,
shishira

Volker Dobler

unread,
May 17, 2020, 3:15:46 PM5/17/20
to golang-nuts
Go, the language and the std tooling has no predefined
way of doing this. Do whatever fits your need.

V.

robert engels

unread,
May 17, 2020, 3:40:39 PM5/17/20
to Volker Dobler, golang-nuts
SpringBoot is a framework. How it handles this is different from every other framework.

Easiest way in Go, just name your configuration files similar to filename.mode.yaml where mode is prod, dev, qa, etc. and set a env variable for the mode.

You can then take it from there, and have base configurations, etc.

All that being said, you might look at a Go configuration library like viper and there are many more...

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/8c3072ff-0848-455a-854c-c8f515792dc0%40googlegroups.com.

Amnon Baron Cohen

unread,
May 18, 2020, 1:51:42 AM5/18/20
to golang-nuts
My advice is to use the same application-dev.yml,   application-test.yml, application-prod.yml YAML files 
that you are familiar with, and have your application read its config from them.

Changing language does not mean that you have to change the way you configure your application.

Brian Candler

unread,
May 18, 2020, 5:46:53 AM5/18/20
to golang-nuts
Many projects define their options in a struct { ... }, and then parse a YAML config file into that struct.  See for example:
https://github.com/QubitProducts/exporter_exporter/blob/master/config.go#L30

Aside: I believe there's currently an issue with yaml.v3 which makes it hard to error-out when unexpected fields are seen:

Dimas Prawira

unread,
May 19, 2020, 1:28:44 AM5/19/20
to Shishira Pradhan, golang-nuts
I am also Java Developer using SpringBoot as my framework, I use three kinds of application configuration files (dev, staging, production). But in Go, I / we use Consul  for storing configuration so the configuration file will be only one. For configuration format I use Yaml format using viper (https://github.com/spf13/viper).

Hope that help

Regards
Dimas

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Ronny Bangsund

unread,
May 19, 2020, 8:44:23 AM5/19/20
to golang-nuts
On Sunday, May 17, 2020 at 7:19:23 PM UTC+2, Shishira Pradhan wrote:
In springboot, we have configuration like port, database etc info are stored in yaml files like application-dev.yml,   application-test.yml, application-prod.yml profiles, and profile name is passed during running the application to load profile specific configuration. I need to understand that how to load/pass profile based properties in golang ?
I use Docker a lot at work (everything we do runs on AWS or Google servers), and that makes separating things relatively simple.

AWS has different environment variables for different configurations, allowing one Docker image to run with different settings. With docker-compose you can tie together tailored Dockerfiles and different images for different purposes, as needed. I recommend having a default "docker-compose.yml" be the dev environment, with its own environment variables, and variants for staging/prod with other names expecting envvars from the system it's running on. This makes the default invocations of docker-compose launch in local dev mode.

We use a lot of Python (Django, Wagtail) which expects envvars anyway, so it's a natural way to do things there. For Go I either write AWS-specific things which only expect envvars, or use a configuration system which overrides config files with outside vars.

We haven't started using Kubernetes yet, but the process probably won't be much different.
Reply all
Reply to author
Forward
0 new messages