Unmarshal env to struct.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
.travis.yml
LICENSE
README.md
doc.go
goenv.go
goenv_test.go

README.md

goenv

Build Status codecov

Unmarshal env to struct.

doc

Package goenv is a package to unmarshal environments of the os to a struct object. It must be use tag for fields. The tag name is env. For example:

type Number struct {
    One int `env:"one"`
    Two float32 `env:"two"`
}

supported type

  • bool, []bool
  • string, []string
  • int, []int
  • int8, []int8
  • int16, []int16
  • int32, []int32
  • int64, []int64
  • uint, []uint
  • uint9, []uint9
  • uint16, []uint16
  • uint32, []uint32
  • uint64, []uint64
  • float32, []float32
  • float64, []float64
  • struct

For bool type, if the environment is not set, the value is false. Otherwise, the value is true.

field tag

  • env envirnoment name
  • envdef default value if unseted.
  • envsep if a slice, use this to split items. Default is ,

The Field must be exported if want to unmarshal.

example

import "fmt"
import "github.com/tenfyzhong/goenv"

type Number struct {
	zero   int    `env:"zero"`
	One    int    `env:"one"`
	Two    int    `env:"two"`
	Three  bool   `env:"three"`
	Four   string `env:"four"`
	Five   string
	Six    *int8   `env:"six"`
	Sevent uint    `env:"sevent"`
	Eight  float32 `env:"eight"`
	Nine   bool    `env:"nine"`
	Ten    *bool   `env:"ten"`
    Substruct *struct {
        S1 int `env:"s1" envdef:"1"`   // "substruct.s1"
        S2 []int `env:"s2" envdef:"1,2"` // "substruct.s2"
        S3 []int `env:"s3" envdef:"1:2" envsep:":"` // "substruct.s2"
    } `env:"substruct"`
}

func main() {
    n = &Number{}
    err := goenv.Unmarshal(n)
    if err == nil {
        fmt.Println(n)
    }
}