caarlos0/env

UseFieldNameByDefault resulting in incorrect Field Name

Closed this issue · 1 comments

given these config:

type Config struct {
	BACKEND_URL   string `envDefault:"localhost:8000"`
}

and using:
opts := env.Options{UseFieldNameByDefault: true}
My app crashes because it cant find the env var BACKENDURL. I think the underscore should be kept given that it is the convention for writing env vars

you're correct that that is the convention for environment variable names, but it's not the convention for struct fields.

If you do instead:

type Config struct {
	BackendURL   string `envDefault:"localhost:8000"`
}

it'll get the name you expect.

I just pushed some documentation improvements around that, and a test to show how it works: