okconf is a simple, unambitious config library that gets out of the way. It was created as an alternative to contrast libraries such as spf13/viper, which — while extremely potent — require a fair amount of ceremony in order to get going.
okconf takes a more minimalist approach. Define your configuration as a struct
, implement a Default()
for it, and okconf will handle loading from and saving to JSON or YAML.
Despite its simplicity, okconf supports complex nested configuration structures and many different data types.
Add okconf to your project:
$ go get github.com/aevitas/okconf
First, define your configuration as a struct
:
type ExampleConfig struct {
Name string
StartedAt time.Time
Nested NestedConfig
}
type NestedConfig struct {
Count int
}
The only mechanism okconf relies on is a Default
method it can use to establish a starting point for your configuration:
func (e ExampleConfig) Default() Config {
return ExampleConfig{
Name: "Foo",
StartedAt: time.Now().UTC(),
Nested: NestedConfig{
Count: 100,
},
}
}
Then, simply Load
your config from your preferred source. For instance, to load from a JSON file:
cfg, err := okconf.LoadJSON[ExampleConfig]("/path/to/file")
The cfg
variable now holds an ExampleConfig
value with the values in the JSON file applied to the default config.
okconf provides various methods to save your config, e.g.:
okconf.SaveJSON(cfg, "file.json")