Skip to content

Conversation

@sunjayaali
Copy link
Contributor

This PR is too add new method like os.GetEnv but with default value.

Closes #54

// the type assertion is valid for the expected type of the environment variable.
func Env[T any](key string, defaultValue T) T {
if v, ok := os.LookupEnv(key); ok {
return any(v).(T)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this cast won’t work for many types like string to an integer for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. Need to change to constraint and how to cast the value. But at least the signature will be like that.

@sunjayaali sunjayaali closed this Dec 15, 2025
@sunjayaali sunjayaali deleted the os/envordefault branch December 15, 2025 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add env or default function

2 participants