Appending values to existing values of environment variables in go

Issue

How can I append another value to an existing value of a go environment variable?

If CGO_CXXFLAGS has the value "-I/blah/blah"

Since the following doesn’t work

$ go env -w CGO_CXXFLAGS="$CGO_CXXFLAGS -I/foo/bar"

I want to find a proper way to CGO_CXXFLAGS have the value "-I/blah/blah -I/foo/bar"

by avoiding to simply re-set all the values plus the new one, such as in:

$ go env -w CGO_CXXFLAGS="-I/blah/blah -I/foo/bar"

Solution

Since go env <var_name> outputs the effective setting of the variable (i.e. not necessarily the default value), a reliable way to get at the current default value of an environment variable is using a subshell (assuming a Bash shell):

$ (unset <var_name>; go env <var_name>)

And then, to set a new value you’d want to use command substitution:

$ go env -w <var_name>="$(unset <var_name>; go env <var_name>)<value_to_append>"

See the following annotated log for a working example:

# Write an initial default.
$ go env -w CGO_CXXFLAGS=-I/blah/blah

$ go env CGO_CXXFLAGS
-I/blah/blah

# Keep in mind that the default can be overridden by a currently set 
# environment variable.
$ export CGO_CXXFLAGS="-I/override/default"

$ go env CGO_CXXFLAGS
-I/override/default

# To print the default value even when it is being overridden,
# in a subshell, unset the environment variable and then run 'go env'. 
$ (unset CGO_CXXFLAGS; go env CGO_CXXFLAGS)
-I/blah/blah

# To set a new default value utilizing the existing default value.
#
# Note: if the default isn't being overridden, no warning will be printed.
$ go env -w CGO_CXXFLAGS="$(unset CGO_CXXFLAGS; go env CGO_CXXFLAGS) -I/foo/bar"
warning: go env -w CGO_CXXFLAGS=... does not override conflicting OS environment variable

# State after change...
# Default value
$ (unset CGO_CXXFLAGS; go env CGO_CXXFLAGS)
-I/blah/blah -I/foo/bar

# Overridden value
$ go env CGO_CXXFLAGS
-I/override/default

Alternatively, you could find the file with the defaults and parse it directly (go env GOENV will tell you the file location), but I think it’s easier to temporarily unset the environment variable and let go env do the work.


Equivalent in PowerShell:

# Write default.
PS > go env -w CGO_CXXFLAGS=-I/blah/blah
PS > go env CGO_CXXFLAGS
-I/blah/blah

# Override default.
PS > $Env:CGO_CXXFLAGS = "-I/override/default"
PS > go env CGO_CXXFLAGS
-I/override/default

# Print default.
PS > Start-Job {$Env:CGO_CXXFLAGS=''; go env CGO_CXXFLAGS} | Receive-Job -Wait
-I/blah/blah

# Append to default.
PS > go env -w CGO_CXXFLAGS="$(Start-Job {$Env:CGO_CXXFLAGS=''; go env CGO_CXXFLAGS} | Receive-Job -Wait) -I/foo/bar"
warning: go env -w CGO_CXXFLAGS=... does not override conflicting OS environment variable

# Final state.
PS > go env CGO_CXXFLAGS
-I/override/default

PS > Start-Job {$Env:CGO_CXXFLAGS=''; go env CGO_CXXFLAGS} | Receive-Job -Wait
-I/blah/blah -I/foo/bar

Answered By – chuckx

Answer Checked By – David Goodson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.