英文:
Appending values to existing values of environment variables in go
问题
如何将另一个值追加到现有的 go 环境变量的值中?
如果 CGO_CXXFLAGS 的值为 "-I/blah/blah"
由于以下方法不起作用
$ go env -w CGO_CXXFLAGS="$CGO_CXXFLAGS -I/foo/bar"
我想找到一种合适的方法,使 CGO_CXXFLAGS 的值为 "-I/blah/blah -I/foo/bar"
而不是简单地重新设置所有的值加上新的值,例如:
$ go env -w CGO_CXXFLAGS="-I/blah/blah -I/foo/bar"
英文:
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"
答案1
得分: 3
由于go env <var_name>
输出的是变量的有效设置(即不一定是默认值),获取环境变量当前默认值的可靠方法是使用子shell(假设使用Bash shell):
$ (unset <var_name>; go env <var_name>)
然后,要设置新值,您可以使用命令替换:
$ go env -w <var_name>="$(unset <var_name>; go env <var_name>)<value_to_append>"
请参考以下带注释的日志,了解一个工作示例:
# 写入初始默认值。
$ go env -w CGO_CXXFLAGS=-I/blah/blah
$ go env CGO_CXXFLAGS
-I/blah/blah
# 请记住,默认值可能被当前设置的环境变量覆盖。
$ export CGO_CXXFLAGS="-I/override/default"
$ go env CGO_CXXFLAGS
-I/override/default
# 要打印默认值,即使它被覆盖了,在子shell中取消设置环境变量,然后运行'go env'。
$ (unset CGO_CXXFLAGS; go env CGO_CXXFLAGS)
-I/blah/blah
# 要设置新的默认值,利用现有的默认值。
#
# 注意:如果默认值没有被覆盖,将不会打印警告。
$ 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
# 更改后的状态...
# 默认值
$ (unset CGO_CXXFLAGS; go env CGO_CXXFLAGS)
-I/blah/blah -I/foo/bar
# 被覆盖的值
$ go env CGO_CXXFLAGS
-I/override/default
或者,您可以找到默认值的文件并直接解析它(go env GOENV
将告诉您文件位置),但我认为暂时取消设置环境变量并让go env
完成工作更容易。
在PowerShell中的等效方法:
# 写入默认值。
PS > go env -w CGO_CXXFLAGS=-I/blah/blah
PS > go env CGO_CXXFLAGS
-I/blah/blah
# 覆盖默认值。
PS > $Env:CGO_CXXFLAGS = "-I/override/default"
PS > go env CGO_CXXFLAGS
-I/override/default
# 打印默认值。
PS > Start-Job {$Env:CGO_CXXFLAGS=''; go env CGO_CXXFLAGS} | Receive-Job -Wait
-I/blah/blah
# 追加到默认值。
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
# 最终状态。
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
以上是您提供的内容的翻译。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论