Skip to content

proposal: os: add EnvironMap function #75934

@lbordowitz

Description

@lbordowitz

Proposal Details

There are two primary use-cases for the os.Environ function:

  1. Passing through environment variables to a sub-command or other executable
  2. Parsing all environment variables into a map

The latter requires going through each string in os.Environ(), usually splitting it on the = sign, and then getting the last part. This is perilous and potentially bug-prone: what if there's an "=" character in the value of the environment variable? Re-joining everything ""? It is not efficient. A widespread need with so many different implementations recommends a library standard function to cut through the noise.

The parsing method in the unix syscall.Getenv could be abstracted to provide a method EnvironMap() map[string]string for the entirety of the envs, and the windows syscall GetEnvironmentStringsW can be similarly parsed. I recommend adding this method in the syscall package, and then exposing it in os as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposalWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions