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
+36.3k Golang : Validate IP address
+29.7k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+7.5k Android Studio : AlertDialog to get user attention example
+8.2k Golang : Count leading or ending zeros(any item of interest) example
+40.9k Golang : How to count duplicate items in slice/array?
+10.2k Golang : Wait and sync.WaitGroup example
+9.6k Golang : Detect number of active displays and the display's resolution
+12.2k Golang : Get month name from date example
+12.3k Golang : Search and extract certain XML data example
+13k Golang : How to get a user home directory path?
+12.2k Golang : 2 dimensional array example
+21k Golang : Create and resolve(read) symbolic links