我正在尝试在 golang 中运行一个函数,但出现了一些错误。谁能帮忙。
func exportVal(name string, value string){
var ops = isWindows()
if ops == true{
set name=value
fmt.Printf("name=")
}else{
export name=value
fmt.Printf("name=")
}
}
但是我遇到了以下错误。
D:\Go>go run octa.go
# command-line-arguments
.\octa.go:42:10: syntax error: unexpected name at end of statement
.\octa.go:46:13: syntax error: unexpected name at end of statement
最佳答案
set
和export
不是Go keywords .也许你在想 shell?您似乎正在尝试设置和导出环境变量。
您不能将环境变量从一个进程导出到另一个进程。您只能更改自己的流程环境。子进程会继承父进程的环境,但你不能反过来。您只能在 shell 程序中执行此操作,因为该“程序”实际上是 shell 本身的一组命令,并且只有在使用 source something.sh
时才会这样做。 .相比之下,sh something.sh
是在一个新的 shell 进程中运行的。
如果您想从非 shell 程序“导出”数据,则必须以某种格式打印数据,JSON是一个不错的选择,并让其他进程读取它。
关于function - golang中变量的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52462032/