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
+15k Golang : How to get Unix file descriptor for console and file
+19.2k Golang : How to count the number of repeated characters in a string?
+8k Prevent Write failed: Broken pipe problem during ssh session with screen command
+17.6k Golang : Login and logout a user after password verification and redirect example
+22.6k Golang : simulate tail -f or read last line from log file example
+9.5k Golang : Load ASN1 encoded DSA public key PEM file example
+12.1k Golang : Validate email address
+13.3k Golang : Strings comparison
+5.7k Unix/Linux : How to open tar.gz file ?
+13.7k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+5.6k Cash Flow : 50 days to pay your credit card debt
+30.1k Get client IP Address in Go