Skip to content

Instantly share code, notes, and snippets.

@ripienaar
Created February 13, 2020 13:49
Show Gist options
  • Save ripienaar/882a4b929cc562042f5e964c5c604a43 to your computer and use it in GitHub Desktop.
Save ripienaar/882a4b929cc562042f5e964c5c604a43 to your computer and use it in GitHub Desktop.
20200213_choria_config_blog
c := &Config{}
// Initialize the struct with default values
err := confkey.SetStructDefaults(c)
panicIfErr(err)
// Next we have to set complex types - number, bool, slices etc - from strings as the data comes from the configuration file as text
// the conversions are all handled by the confkey package
// Set the value in the struct for whatever field matches the key loglevel
err = confkey.SetStructFieldWithKey(c, "loglevel", "error")
panicIfErr(err)
// automatically handles the split etc, can be overridden using environment variable SERVERS
err = confkey.SetStructFieldWithKey(c, "servers", "h1.example.net:4222, h2.example.net:4222")
panicIfErr(err)
// validate all the inputs matches validate rules, like the enum list
err = confkey.Validate(c)
panicIfErr(err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment