Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UseFieldNameByDefault resulting in incorrect Field Name #304

Closed
tomaszbk opened this issue Apr 25, 2024 · 1 comment
Closed

UseFieldNameByDefault resulting in incorrect Field Name #304

tomaszbk opened this issue Apr 25, 2024 · 1 comment
Assignees
Labels

Comments

@tomaszbk
Copy link

tomaszbk commented Apr 25, 2024

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

caarlos0 added a commit that referenced this issue Apr 25, 2024
caarlos0 added a commit that referenced this issue Apr 25, 2024
@caarlos0
Copy link
Owner

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:

@caarlos0 caarlos0 self-assigned this Apr 25, 2024
@caarlos0 caarlos0 added the docs label Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants