LoginSignup
81
46

More than 5 years have passed since last update.

golangで環境変数扱う(envconfig)

Posted at

go 勉強してたら kelseyhightower/envconfig と出会い、一通り触ってみました。

golang で環境変数を扱う場合で、 os パッケージ でやるとこんな感じですが

func main() {
    gopath := os.Getenv("GOPATH")
    fmt.Println(gopath) // C:\Users\andromeda\go
}

envconfig だと struct のフィールドから環境変数探してセットしてくれる

type Env struct {
    Gopath string // 先頭大文字
}

func main() {
    var goenv Env
    envconfig.Process("", &goenv)
    fmt.Println(goenv) // {C:\Users\andromeda\go}
}

envconfig.Proces で第一引数に prefixを 第二引数にstructのポインタ型を指定するといい感じに環境変数をセットしてくれる

第一引数で prefix 指定すると、(アンスコ足して)スネークケースにして環境変数を探してくれる(普段は "" にして多分使う事ないと思う)

$ export TEST_HELLO="hello, getenv"
type Env struct {
    Hello string
}

func main() {
    var goenv Env
    envconfig.Process("test", &goenv) // 引数にアンスコを付けて TEST_HELLO で探す
    fmt.Println(goenv)                // {hello, getenv}
}

色んな型

型は struct 側で指定しておけば、良しなにやってくれる

$ export PORT="8080"
$ export BOOL="false"
type Env struct {
    Gopath string
    Port   int
    Bool   bool
}

func main() {
    var goenv Env
    envconfig.Process("", &goenv)
    fmt.Println(goenv.Gopath)                     // C:\Users\andromeda\go
    fmt.Printf("Gopath type: %T\n", goenv.Gopath) // Gopath type: string
    fmt.Println(goenv.Port)                       // 8080
    fmt.Printf("Port type: %T\n", goenv.Port)     // Port type: int
    fmt.Println(goenv.Bool)                       // false
    fmt.Printf("Bool type: %T\n", goenv.Bool)     // Bool type: bool
}

struct のタグでゴニョゴニョ

envconfig:

envconfig:"HOGE" とすると、フィールドは無視され、 $HOGE がセットされる

type Env struct {
    Dir string `envconfig:"GOPATH"`
}

func main() {
    var goenv Env
    envconfig.Process("", &goenv)
    fmt.Println(goenv.Dir) // C:\Users\andromeda\go
}

default:

default:"HOGE" とすると、環境変数がセットされていない場合、 $HOGE がセットされる

$ unset PORT
type Env struct {
    Port int `envconfig:"PORT" default:"80"`
}

func main() {
    var goenv Env
    envconfig.Process("", &goenv)
    fmt.Println(goenv.Port) // 80
}

required:

required:"true" で、環境変数がセットされていない場合、 err が返る

getenv.go
type Env struct {
    Hoge int `required:"true"` // $HOGEは未定義
}

func main() {
    var goenv Env
    if err := envconfig.Process("", &goenv); err != nil {
        log.Printf("[ERROR] Failed to process env: %s", err)
    }
}

 $ go run getenv.go
2018/12/14 12:58:25 [ERROR] Failed to process env: required key HOGE missing value

ignored:

ignored:"true" で環境変数が定義されていても無視される (使うこれ…?)

type Env struct {
    Gopath string `ignored:"true"`
}

func main() {
    var goenv Env
    envconfig.Process("", &goenv)
    fmt.Println(goenv) // {}
}

split_words:

split_words:"true" でstructのキャメルケースと環境変数のスネークケースをいい感じに探してくれる (雑

(structに TestHey って書いたら、環境変数の TEST_HEY を探してくれる)


$ export TEST_HEY="hello, getenv"
type Env struct {
    TestHey string `split_words:"true"`
}

func main() {
    var goenv Env
    envconfig.Process("", &goenv)
    fmt.Println(goenv) // {hello, getenv}
}
81
46
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
81
46