The Best Go Configuration Management Library

Meet Koanf, the Goldilocks Go Library

Mike Riley
The Pragmatic Programmers
3 min readFeb 1, 2022

--

Thanks to fellow Pragmatic Bookshelf authors Ricardo Gerardi and Travis Jeffery, I have been stung by the Go bug. Further cementing my appreciation and use of the language are its performance and ease of deployment, especially compared to my original go-to language, Python.

Powerful Command-Line Applications in Go by Ricardo Gerardi is a quick yet thorough introduction to using Go in the real world:

Distributed Services with Go by Travis Jeffrey is a perfect intermediate compliment to Ricardo’s book for learning more advanced Go concepts:

Configuration Management Needs

As I began to convert some of my own Python scripts into Go apps, I needed a way to easily yet comprehensively manage my program configurations and command-line flags.

While John Barton’s popular godotenv project is super simple for basic environmental variable protection, it currently doesn’t offer any command- line flag management.

📝 I use godotenv all the time for keeping secrets out of my Git-managed source code.

Steve Francia’s Viper is an extremely popular Go library that slices and dices all kinds of configuration file and command-line flag needs, but it’s like the V’Ger spacecraft in Star Trek. It has grown from a humble library into a massive Swiss army knife ready to cut into a broad swath of Go programmer needs. Unfortunately, all this scope creep brings with it a lot of extra features and dependencies that may be overkill for minimalistic Go programs — especially those not intended for high-grade commercial use.

Meet koanf

After a bit more searching through related Go projects, I came across a Goldilocks library that wasn’t too simple and wasn’t too complex — one that was just right. Kailash Nadh’s koanf hit all the advantages I was looking for with Viper without all the heavyweight baggage and dependencies.

Koanf not only reads and watches for changes to external config files stored in HCL, JSON, TOML, or YAML format, but it also elegantly handles command-line flags along with default value settings. The library is also easily extensible for custom configurations.

If you are looking for a lightweight yet powerful configuration management library for your Go programming needs, check out koanf. It’s the best Go configuration management library, and I use it for my Go programs.

Be sure to pick up a copy of Portable Python Projects by Mike Riley, available from The Pragmatic Bookshelf. You can save 35 percent with promo code python_automation_2022 now through March 31, 2022. Promo codes are not valid on prior purchases.

--

--