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
if you go program is another app like program in Mac, then use following to set it:
sudo launchctl setenv <env var> <env var value>
you can have your env variables in a .env file and export them when you run it like this:
export less .env | xargs
; go run cmd/main.go
I just had this issue and I solved it by not running the program as root. I'm not sure of the cause, but it works now!