When it comes to configuration files and parsing them, you have a lot of options, from simple to complicated.
We could, of course, simply store what we want in JSON, but that format is a little tricky to work directly for humans—it will require escaping characters and so on, which makes it vulnerable to errors.
Instead, we'll keep things simple by using a standard ini config
file library in gcfg, which handles gitconfig
files and traditional, old school .ini
format, as shown in the following code snippet:
[revisions] count = 2 revisionsuffix = .rev lockfiles = false [logs] rotatelength = 86400 [alarms] emails = [email protected],[email protected]
You can find gcfg at https://code.google.com/p/gcfg/.
Essentially, this library takes the values of a config file and pushes them into a struct in Go. An example of how we'll do that is as follows:
package main import ( "fmt" "code.google.com/p/gcfg" ) type Configuration struct { Revisions struct { Count int Revisionsuffix string Lockfiles bool } Logs struct { Rotatelength int } Alarms struct { Emails string } } func main() { configFile := Configuration{} err := gcfg.ReadFileInto(&configFile, "example.ini") if err != nil { fmt.Println("Error",err) } fmt.Println("Rotation duration:",configFile.Logs.Rotatelength) }