Documentation ¶
Overview ¶
Set flags via environment variables.
Flags are still defined using the stdlib package flag. The only change to your code is calling envflag.Parse() in place of flag.Parse().
If your flag wasn't set via command-line argument, an equivalent environment variable will be used.
Precedence is: command-line agrument, environment variable, default.
As an example, you can create a new file (main.go)
package main import ( "flag" "log" "github.com/danott/envflag" ) func main() { var i int flag.IntVar(&i, "port", 2112, "Run on this port.") envflag.Parse() log.Printf("port: %v", i) }
Run your example to see the precedence in action:
go run main.go go run main.go --port=2113 PORT=2114 go run main.go PORT=2114 go run main.go --port=2113
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var EnvPrefix = ""
Configure how flag names are translated to environment variable names by prefixing the flag name.
View Source
var FlagSet = flag.CommandLine
The flag.FlagSet to act on.
Functions ¶
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.