import "github.com/dmotylev/appconfig"
See GoDoc.
Set environment variables:
APP_TIMEOUT=1h2m3s
APP_WORKERNAME=Monkey
Write other values to the file:
cat > local.conf << EOF
APP_TIMEOUT=2h2m3s
APP_NUMWORKERS=10
APP_WORKERNAME=Robot
EOF
Write code:
Populate variable from both sources:
package main
import (
"fmt"
"time"
"github.com/dmotylev/appconfig"
)
func main() {
var conf struct {
Timeout time.Duration
Day time.Time `time,format:"2006-01-02"`
WorkerName string
NumWorkers int
}
err := appconfig.Load(&conf, appconfig.FromEnv("APP_"), appconfig.FromFile("local.conf"))
fmt.Printf("err=%v\n", err)
fmt.Printf("timeout=%s\n", conf.Timeout)
fmt.Printf("day=%s\n", conf.Day.Format(time.UnixDate))
fmt.Printf("worker=%s\n", conf.WorkerName)
fmt.Printf("workers=%d\n", conf.NumWorkers)
}
Results:
err=<nil>
timeout=1h2m3s
day=Fri Dec 13 00:00:00 UTC 2013
worker=Monkey
workers=10
Get some inspiration from tests.
The package available under LICENSE.