-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
proposal: os: Splitenv #62038
Comments
cc @bcmills @golang/windows |
Wouldn't |
@Jorropo, there are no meaningful error messages to report. Either the entry contains a non-leading Per #52436 (comment), on some platforms it may be valid for |
Wouldn't it error if I pass it something like |
One question, though. If An implementation by analogy with |
No. It would return with |
If it returns This was just a suggestion, if this is evident to anyone that needs to use this function the use |
@Jorropo Both approaches have precedent in existing language and library features, and I agree that the tradeoff between them is subtle. A A (Neither analogy is perfect because in principle Go functions can return both a result and an error, or return a result even when the boolean indicator is With that said then, I understand this proposal as being for a function that represents "If the string follows the conventions of a key/value pair, return the separated key and value; otherwise <do something>" -- the "do something" part is a subject of active discussion above, but one example I see above is "otherwise return the whole string as the value and an empty key". In that case, the boolean result is not indicating success or failure but instead indicating whether the predicate held. It also seems a valid interpretation to define this as "try to parse this as a key/value pair", and consider the inability to do so as an error. But I think the assumption here is that even if we do think of this as a failure rather than just one of two possible outcomes, there is only one possible error case and no evidence that there will be any new error cases in future, and so this function would presumably always return a single fixed error and callers would only ever compare it to FWIW I don't have a strong opinion either way, but both of these designs feel functionally equivalent to me and so I'm therefore inclined to marginally prefer the simpler of the two, which is to return a boolean directly. |
Most code can and should call |
It is kinda annoying that the platform-agnostic abstraction provided by Out of curiosity I searched the main codebase I maintain in my day job and found several uses of Several of them are taking that result, appending new entries to it, and passing the result into the Three of the uses seem potentially problematic:
Based on this analysis, my codebase in particular could use I don't mean to imply that this wouldn't be useful to other programs, but I did think it was interesting that this program in particular is using For my purposes, documentation would be sufficient although I'd prefer that documentation to describe the specific situations where these odd environment entries are expected rather than just make a general allusion to "you might see this sort of thing", since it seems like it's not actually important to handle that exception in practice unless you are particularly interested in interacting with this implementation detail of |
I'd like to propose adding:
This issue is a continuation of #61956 where, after some archeology, it was re-discovered that on Windows, environment variables have one of two forms. In addition to the well-known
key=value
, the form=key=value
is allowed and indeed used bycmd.exe
.Problems related to
=key=value
entires have been previously encountered in #49886 and #52436.I believe that this belongs in the standard library, so that more Go programs are portable across platforms, and so that people do not have to re-invent this particular elliptical wheel whenever they port their stack to Windows and notice something unusual.
In addition, although this may be a separate proposal due to the difficulty of detecting this reliably,
go vet
might detect some common, incorrect attempts to implementSplitenv
based onstrings.Cut
or similar functions.Lastly I would suggest that documentation of
os.Environ
mentionsos.Splitenv
, so that developers are pointed towards the right code and, perhaps, refrain from implementing this function by hand.I'm happy to implement the function, tests or anything else required.
The text was updated successfully, but these errors were encountered: