kelseyhightower / envconfig

Golang library for managing configuration data from environment variables
MIT License
5.07k stars 382 forks source link

Getting some strange behaviour when setting both envconfig: and default: tag on a single field #26

Closed robvdl closed 8 years ago

robvdl commented 9 years ago

I want to use both the tags envconfig:"multi_word_var" and default:"value" together on a struct field but it doesn't seem to work and I am getting some strange behaviour when I combine these. The behaviour even changes when you change the order of them (either default: first or envconfig: first).

It is working fine when I am only using one of these tags on a field, but it seems to not work properly when I put both tags on a field.

I was expecting it would try to use the environment variable first (as defined by the envconfig: tag) and if that didn't exist, use the default value defined by the default: tag. This is not the behaviour I am getting however.

It is looking like you can't use these tags together.on a single field.

dimfeld commented 8 years ago

This works fine for me. Since you say the order matters, maybe you are specifying the tags incorrectly and the Go parser is only picking up the first one? How did you write it out?

EDIT: I tried separating them with a comma and found that Go does not pick up the second one if you do this. They need to be separated with whitespace instead. That is, you should write envconfig:"multi_word_var" default:"value", not envconfig:"multi_word_var", default:"value". Maybe that's the issue?

EDIT2: Here's a Go playground example that illustrates this: http://play.golang.org/p/8eAmIte-_d And the relevant spec: https://golang.org/pkg/reflect/#StructTag. Apparently this is just a "convention" used by the reflect package, rather than a strictly enforced rule, which is probably why you don't get an error. I did notice that go vet raises a warning on improperly-formatted struct tags though.

robvdl commented 8 years ago

I see, sorry about this, closing the issue then.