0

OSX Catalina & zsh as my terminal.

I have a command to start a psql connection :

db, err := pgx.Connect(context.Background(), os.Getenv("PSQL_URL"))

But the os.Getenv("PSQL_URL") is an empty string.

How to make sure go program can read my environment variable ?

In the terminal, if I echo $PSQL_URL I get the proper postgresql://aod:toto@localhost/dbname

If I export PSQL_URL="postgresql://aod:toto@localhost/dbname" before running main.go it works fine

I'm looking for a persistent way of doing it.

UPDATE

My mistake was as follow : Inside ~/.zhsrc I did set PSQL_URL="postgresql://aod:toto@localhost

instead of

export PSQL_URL="postgresql://aod:toto@localhost"

Ado Ren
  • 3,511
  • 4
  • 21
  • 36
  • 2
    *"If I export ... before running main.go it works fine"* That's the way to do it. Only exported variables become available in child processes. This has nothing to do with go, zsh or mac os. You can try it simply by running `echo $PSQL_URL` in a sub process. See https://stackoverflow.com/questions/1158091/defining-a-variable-with-or-without-export – Felix Kling Oct 16 '19 at 21:32
  • But I have to export it everytime I run the program. Is there not a way to make it persistent ? I'm used to set my env variables in `/etc/environment` in linux and `os.Getenv` can read them just fine – Ado Ren Oct 16 '19 at 21:33
  • 1
    I don't know how the terminal/shell inside VSCode works, sorry. Is it an actual shell and you do something like `go build; ./command` in it to run the app? – Felix Kling Oct 16 '19 at 21:37
  • oh... I see. I used to `go build` all the time on linux. Whereas `go run main.go` uses shell variables. Thanks for the heads up ! – Ado Ren Oct 16 '19 at 21:39
  • `go run main.go` likely works just fine as well. The point I wanted to get was to whether the terminal sessions that VSCode creates are short-living (e.g. only exist for one build) or whether it's a single session across the lifetime of VSCode. There might be a VSCode specific solution. – Felix Kling Oct 16 '19 at 21:41
  • hmm actually tested after a reboot and it does not matter if I use the regular terminal / vscode terminal (it is an actual shell) - nor if I use the binary created from `go build`. Kinda confused, still looking – Ado Ren Oct 16 '19 at 21:45
  • Tried to echo $PSQL_URL in a sub process and it returns the proper variable. – Ado Ren Oct 16 '19 at 21:47
  • Maybe we are talking about two different things here. I have an example app that just does `fmt.Println(os.Getenv("FOO"))`. I can do `export FOO="foo"` *once* and call the command multiple times after that (in the same shell) and see the value of the env variable every time. I.e `export FOO="foo"; ./test; ./test; ./test`. (note: normal terminal, not vscode) What are you doing? – Felix Kling Oct 16 '19 at 21:51
  • Let's say I open a new terminal and then try to run `fmt.Println(os.Getenv("PSQL_URL"))` from `./main`. Printed variable will be an empty string. If I export `PSQL_URL="toto@url"` then run main it will work just fine. What I usually do on my linux computer is setting environment variables in `/etc/environment` and I never have to export, hence my surprise reading your comment. – Ado Ren Oct 16 '19 at 22:00
  • 1
    Ah, I think I understand now. You want this env variable to exist "automatically" for every shell you are starting, is that correct? In that case, add `export PSQL_URL="..."` to your `~/.zshrc` file. This file is read whenver zsh is started. – Felix Kling Oct 16 '19 at 22:03
  • thanks guys. My mistake was that in my `~/.zshrc` I had `PSQL_URL="..."` but not `export PSQL_URL="..."`, should work now. – Ado Ren Oct 16 '19 at 22:05

1 Answers1

1

That's the correct way of reading an env variable in go.

The only thing that comes to my mind about what might be happening is that you have two different terminal sessions. In one of them you set PSQL_URL (you are running the echo in this one) and in the other not (you are running your go app here).

EDIT

Expanded now with the your comments and Felix's, the problem here is that you export a variable, but don't make it permanent, so when you start a new session the variable doesn't exist. What you need to do is edit your ~/.zshrc and add export PSQL_URL=foo, this way, whenever you start a new terminal session, the variable will be loaded.

  • All from the same terminal session (inside VSCode), using default zsh. I'm a bit lost as to how solve this – Ado Ren Oct 16 '19 at 21:29