项目作者: GoBike

项目描述 :
Simple environment-variables extension to Golang flag.
高级语言: Go
项目地址: git://github.com/GoBike/envflag.git
创建时间: 2016-08-30T06:56:32Z
项目社区:https://github.com/GoBike/envflag

开源协议:MIT License

下载


envflag

Go Report Card
GoDoc

Simple environment extension to Golang flag.

Goals

  • Extends Golang flag with environment-variables.
  • Clear precendence: default < environment-variable < cli.
  • Adheres to 12-factor-app.

Installation

  1. $ go get github.com/gobike/envflag

Usage

Create main.go

  1. package main
  2. import (
  3. "fmt"
  4. "flag"
  5. "github.com/gobike/envflag"
  6. )
  7. func main() {
  8. var (
  9. times int
  10. )
  11. flag.IntVar(×, "f-times", 1, "this is #")
  12. envflag.Parse()
  13. fmt.Println(times)
  14. }

Run with default.

  1. $ go run main.go
  2. 1 #output

Run with environment-variable set.

  1. $ F_TIMES=100 go run main.go
  2. 100 #output

Run with cli set.

  1. $ F_TIMES=100 go run main.go --f-times=10
  2. 10 #output, environment-variable is ignored