Skip to content

Unmarshal using ENV without binding #688

@forward32

Description

@forward32

Hi all,

Having this code:

type RpcConfig struct {
	ServerAddr string `mapstructure:"ADDR"`
}

func main() {
	v := viper.New()
	v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
	v.AutomaticEnv()

	//v.BindEnv("ADDR")

	c := &RpcConfig{}
	if err := v.Unmarshal(c); err != nil {
		panic(err)
	}

	log.Printf("conf=%+v", c)
}

and running in this way:

ADDR="test" go run main.go

I got config with empty ServerAddr. If I remove comment from binding line it works but why viper couldn't get it from mapstructure tag itself?

Please let me know if I doing something wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions