Skip to content

proposal: os: Splitenv #62038

@zyga

Description

@zyga

I'd like to propose adding:

// Splitenv splits a key=value entry, as found in the environment block.
//
// Splitenv understands Windows-specific environment entries that contain
// a leading '=' in the key name.
func Splitenv(s string) (key, value string, ok bool) {
   ...
}

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 by cmd.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 implement Splitenv based on strings.Cut or similar functions.

Lastly I would suggest that documentation of os.Environ mentions os.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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions