https://www.cnblogs.com/baiyuxiong/p/4545014.html
https://www.itread01.com/articles/1490395087.html
package main
import "os"
import "fmt"
func main() {
os.Clearenv()
os.Setenv("FOO", "1")
fmt.Println("FOO:", os.Getenv("FOO"))
os.Unsetenv("FOO")
fmt.Println("FOO:", os.Getenv("FOO"))
os.Setenv("hello", "1")
os.Setenv("world", "2")
fmt.Println()
for _, e := range os.Environ() {
fmt.Println(e)
}
}
输出结果:
FOO: 1
FOO:
hello=1
world=2
可以看出,调用Unsetenv后FOO就没有值了。通过for range可以遍历Environ中所有的环境变量。
上述操作,包括Clearenv只会影响程序当前运行环境中的环境变量。并不会对程序外的系统环境变量产生影响。所以当這段代码运行结束时,系统的环境变量并不会发生改变。