I am trying to run one function in golang but I am getting some errors. Can anyone please help.
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=")
}
}
But I am getting below error.
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
Go doesn't support the export
keyword.
Are you trying to create a variable with global scope?
var foo string
func SetFoo(to string) {
foo = to
}
Or are you trying to set an environment variable? In which case, use os.Setenv(key, value)
.
set
and export
are not Go keywords. Maybe you're thinking of shell? It looks like you're trying to set and export environment variables.
You cannot export environment variables from one process to another. You can only change your own process's environment. Child processes will inherit the parent's environment, but you can't go the other way. You can only do it in a shell program because that "program" is really a set of commands to the shell itself, and only then when using source something.sh
. sh something.sh
, in contrast, is run in a new shell process.
If you want to "export" data from a non-shell program, you'll have to print out the data in some format, JSON is a good choice, and have that be read by the other process.