Golang : How to deal with configuration data?
A friend ask what are the ways to deal with configuration data in Golang. Couple of ways that I can think up are :
The simplest. Read configuration data from key-value pair file. See example on Golang Key-Pair value file.
Read data from YAML file with goyaml.
TOML - see https://github.com/BurntSushi/toml
Read from environment variables.
Hard code into the program source code. Which is not advisable..but if you're writing a small program or microservice..then it is ok. It will make life easier for future maintenance.
UPDATE :
- Thanks to Mark for pointing out the Viper package. https://github.com/spf13/viper. From the Viper's documentation - "When building a modern application, you don’t want to worry about configuration file formats; you want to focus on building awesome software. Viper is here to help with that."
Reference :
https://www.socketloop.com/tutorials/golang-read-data-from-config-file-and-assign-to-variables
See also : Golang : Read data from config file and assign to variables
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.8k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+6.2k Golang : Get missing location after unmarshal binary and gob decode time.
+8.2k Golang : Randomize letters from a string example
+7.5k Android Studio : How to detect camera, activate and capture example
+27.8k PHP : Count number of JSON items/objects
+5.3k JavaScript/JQuery : Redirect page examples
+12.2k Golang : Detect user location with HTML5 geo-location
+27.3k Golang : Find files by name - cross platform example
+3.8k Golang : Switch Redis database redis.NewClient
+5.4k Golang : Pad file extension automagically
+16.5k Golang : Convert slice to array
+11.6k Golang : Change date format to yyyy-mm-dd