Go Configure – Josh Betz https://josh.blog/2017/04/go-configure
Go Configure
I’ve been dabbling in Golang on and off for a little while now. Recently I was looking for a package to read and parse configuration files with the following requirements in mind:
- Be fast.
- Use environment variables and JSON files (in that order).
- Support hot reloading.
I looked at some of the popular configuration packages, but didn’t see anything that I loved. Many of them don’t meet the requirements I had in mind or are more complex than I’d like.
So, I decided to write config — I know, it’s a great name. It’s pretty simple. We cache values in memory to minimize disk IO. On SIGHUP
(or any time you call config.Reload()
), we clear the cache, so you can update the configuration without restarting your app.
Usage looks something like this:
There are more examples and details on the API in the README on Github.
If you have questions or ideas, issues and pull requests are welcome on Github. Let me know if you use it!
package main import ( "fmt" "log" "net/http" "github.com/joshbetz/config" ) func main() { c := config.New("config.json") http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { var value string err := c.Get("value", &value) if err != nil { fmt.Fprintf(w, "Error: %s ", err) return } fmt.Fprintf(w, "Value: %s ", value) }) log.Fatal(http.ListenAndServe(":3000", nil)) }