Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
proposal: os: add function to return environment var with default value #35696
Adding a very small QoL function to go giving the ability to return an environment variable with a possible fallback in one line
Since go doesn't support Overloading or Optional parameters (as far as I am aware)
the added benefit is this way it's also a non-breaking change
Which of the two do you mean? Either seems reasonable, and I don't think there's a clear winner.
I think it's best to let the developer write another three lines and make it explicit and simple. If you want to avoid the extra typing, just set up an editor macro or copy-paste.
@timoheijne You missed or at least did not respond to the semantic difference that @mvdan was pointing out. Some programs will want to distinguish unset from empty; others will not. That's why both os.Getenv and os.LookupEnv exist. If you want to also set a default in one case or the other, it seems reasonable to ask the developer to write out which matters, explicitly.
Or, you know, write a function. :-)