Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 无法获取环境值_Go_Cgo - Fatal编程技术网

Go 无法获取环境值

Go 无法获取环境值,go,cgo,Go,Cgo,使用setenv设置环境值时,os.Getenv无法获取该值。有人能告诉我为什么吗 package main /* #include <stdlib.h> #include <stdio.h> */ import "C" import "os" import "strings" import "fmt" func main() { C.setenv(C.CString("CSET"), C.CString("1.1.1.1:1111"), C.int(1))

使用setenv设置环境值时,os.Getenv无法获取该值。有人能告诉我为什么吗

package main
/*
#include <stdlib.h>
#include <stdio.h>
*/
import "C"

import "os"
import "strings"
import "fmt"

func main()  {
    C.setenv(C.CString("CSET"), C.CString("1.1.1.1:1111"), C.int(1)) //call setenv 
    C.puts(C.getenv(C.CString("CSET")))  // call getenv

    addrs := strings.Split(os.Getenv("CSET"), ";")
    fmt.Printf("addrs = %v\n", addrs)

    os.Setenv("GOSET", "2.2.2.2:2222")
    C.puts(C.getenv(C.CString("GOSET")))
}
这是
Getenv
的一项功能

在源文件中,我们看到在程序初始化阶段创建了环境变量的副本(
envs[]string=runtime\u envs()

如果使用C函数在后台修改程序环境变量,则不会更新此副本

遗憾的是,无法绕过此功能。

请参阅和
dingrui@dingrui-PC:~/Projects/Temp$ go run env.go 
1.1.1.1:1111
addrs = []
2.2.2.2:2222