Simple environment extension to Golang flag.
- Extends Golang flag with environment-variables.
- Clear precendence:
default
<environment-variable
<cli
. - Adheres to
12-factor-app
.
$ go get github.com/gobike/envflag
Create main.go
package main
import (
"fmt"
"flag"
"github.com/gobike/envflag"
)
func main() {
var (
times int
)
flag.IntVar(×, "f-times", 1, "this is #")
envflag.Parse()
fmt.Println(times)
}
Run with default
.
$ go run main.go
1 #output
Run with environment-variable
set.
$ F_TIMES=100 go run main.go
100 #output
Run with cli
set.
$ F_TIMES=100 go run main.go --f-times=10
10 #output, environment-variable is ignored