buildgolang.org/x/build/internal/envutil Index | Files

package envutil

import "golang.org/x/build/internal/envutil"

Package envutil provides utilities for working with environment variables.

Index

Functions

func Dedup

func Dedup(goos string, env []string) []string

Dedup returns a copy of env with any duplicates removed, in favor of later values. Items are expected to be on the normal environment "key=value" form.

Keys are interpreted as if on the given GOOS. (On Windows, key comparison is case-insensitive.)

func Get

func Get(goos string, env []string, key string) string

Get returns the value of key in env, interpreted according to goos.

func Match

func Match(goos, kv, key string) (value string, ok bool)

Match checks whether a "key=value" string matches key and, if so, returns the value.

On Windows, the key comparison is case-insensitive.

func SetDir

func SetDir(cmd *exec.Cmd, dir string)

SetDir sets cmd.Dir to dir, and also updates cmd.Env to ensure that PWD matches.

If dir is the empty string, SetDir clears cmd.Dir and sets PWD to the current working directory.

func SetEnv

func SetEnv(cmd *exec.Cmd, kv ...string)

SetEnv sets cmd.Env to include the given key=value pairs, removing any duplicates for the key and leaving all other keys unchanged.

(Removing duplicates is not strictly necessary with modern versions of the Go standard library, but causes less confusion if cmd.Env is written to a log — as is sometimes done in packages within this module.)

func Split

func Split(kv string) (key, value string)

Split splits a "key=value" string into a key and value.

Source Files

envutil.go

Version
v0.0.0-20250421191922-3619c213cff3 (latest)
Published
Apr 21, 2025
Platform
linux/amd64
Imports
4 packages
Last checked
3 months ago

Tools for package owners.