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
+21.7k Golang : Strings to lowercase and uppercase example
+5.6k Golang : Embedded or data bundling example
+15.6k Golang : How to generate QR codes?
+16k Golang : Check if IP address is version 4 or 6
+24.5k Golang : How to write CSV data to file
+7.4k Golang : Implementing class(object-oriented programming style)
+14.6k Golang : Read large file with bufio.Scanner cause token too long error
+16.6k Golang : Aligning strings to right, left and center with fill example
+9.1k Golang : Read file and convert content to string
+11.3k Golang : Search and extract certain XML data example
+5k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+6.1k Golang : Transform lisp or spinal case to Pascal case example