env: github.com/caarlos0/env Index | Examples | Files

package env

import "github.com/caarlos0/env"



Package Files



var (
    // ErrNotAStructPtr is returned if you pass something that is not a pointer to a
    // Struct to Parse
    ErrNotAStructPtr = errors.New("env: expected a pointer to a Struct")

nolint: gochecknoglobals

func Parse Uses

func Parse(v interface{}, opts ...Options) error

Parse parses a struct containing `env` tags and loads its values from environment variables.


type inner struct {
    Foo string `env:"FOO" envDefault:"foobar"`
type config struct {
    Home         string `env:"HOME,required"`
    Port         int    `env:"PORT" envDefault:"3000"`
    IsProduction bool   `env:"PRODUCTION"`
    Inner        inner
os.Setenv("HOME", "/tmp/fakehome")
var cfg config
if err := Parse(&cfg); err != nil {
    fmt.Println("failed:", err)
fmt.Printf("%+v", cfg)


{Home:/tmp/fakehome Port:3000 IsProduction:false Inner:{Foo:foobar}}

func ParseWithFuncs Uses

func ParseWithFuncs(v interface{}, funcMap map[reflect.Type]ParserFunc, opts ...Options) error

ParseWithFuncs is the same as `Parse` except it also allows the user to pass in custom parsers.


type thing struct {
    desc string

type conf struct {
    Thing thing `env:"THING"`

os.Setenv("THING", "my thing")

var c = conf{}

err := ParseWithFuncs(&c, map[reflect.Type]ParserFunc{
    reflect.TypeOf(thing{}): func(v string) (interface{}, error) {
        return thing{desc: v}, nil
if err != nil {


my thing

type Options Uses

type Options struct {
    // Environment keys and values that will be accessible for the service.
    Environment map[string]string
    // TagName specifies another tagname to use rather than the default env.
    TagName string
    // contains filtered or unexported fields

Options for the parser.

type ParserFunc Uses

type ParserFunc func(v string) (interface{}, error)

ParserFunc defines the signature of a function that can be used within `CustomParsers`

Package env imports 10 packages (graph) and is imported by 135 packages. Updated 2020-07-24. Refresh now. Tools for package owners.