I have the case dosen't work function
os.GetEnv()
I have set a variable in my system ADDR="192.168.1.100" trought file .bashrc and .profile. So if I open terminal and type below command, I get good result
$ echo $ADDR
192.168.1.100
Why in below very simply program I get Error if variable is correct set in system ?
func main(){
addr := os.Getenv("ADDR")
if addr == "" {
return errors.New("missing addres")
}
}
I also restarted IDE a many times. Tried write in terminal again
$ env ADDR="192.168.1.100"
but still this same effect.
I think the problem is likely that you are not exporting the variable, so the sub process (i.e. you ide, shell, is not getting it).
ADDR="192.168.1.100" go run main.go
or
export ADD="192.168.1.100"
go run main.go
I had same problem and for me it's because I run the program with sudo.
I checked the environment variable as a normal user. But run the program as root. So their environment could not be the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With