A Go (Golang) library for managing configuration data from environment variables which is used by my Twelve-Factor Apps.
Download and install it:
go get -u github.com/sboehmann/envconf
Import it in your code:
import "github.com/sboehmann/envconf"
First you need a few environment variables:
export MY_PORT=8080
export MY_MESSAGE="Hello World!"
And now a simple program that uses these variables:
package main
import (
"github.com/sboehmann/envconf"
"io"
"net/http"
)
func message(w http.ResponseWriter, r *http.Request) {
value, _ := envconf.GetString("MESSAGE")
io.WriteString(w, value)
}
func main() {
envconf.SetPrefix("MY_")
http.HandleFunc("/", message)
http.ListenAndServe(":"+envconf.MustGetString("PORT"), nil)
}