Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.GetEnv() dosen't work after set env variable

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.

like image 246
Mbded Avatar asked Sep 21 '16 17:09

Mbded


2 Answers

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
like image 109
Kyle Brandt Avatar answered Sep 21 '22 19:09

Kyle Brandt


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.

like image 39
kim yong bin Avatar answered Sep 20 '22 19:09

kim yong bin