OS environment variable weird behavior

Hello guys,

I have an issue with environment variables that I am not able to solve, namely. os.Environ returns a slice of key-value pair, which correctly shows the values I set in the bash shell, however when I try to access them using os.Getenv the method returns null and what’s worse, os.LookupEnv returns ok == true, but the variable is not having any value. What is going on? Any Ideas?

Go 1.13
Default Bash 3.2.57 (Apple edition)
MacOS Mojave 10.14.6

Does it happen with a particular environment variable ?
Can you provide us with some example so we can try to reproduce what you see ?

By the way, os.Getenv() returns a string. It can’t return null unless the value of the environment variable is "null".

Yes, you are correct, I see empty strings even if I see everything set.

I see the following output:

~/workspace/x-cli :) set | grep 'X'
X_ORGANIZATION=nah
X_PASSWORD=notmypassword
X_REPOSITORY=wooot
X_USERNAME=wooot 
~/workspace/x-cli :) printenv | grep 'X'
X_USERNAME=wooot
X_REPOSITORY=wooot
~/workspace/x-cli :) ./bin/x-cli | grep 'X'
X_USERNAME=
X_REPOSITORY=
X_ORGANIZATION=
X_PASSWORD=

and the only issue I can think of is that something is somewhere cached and it did not propagate throughout the system and I don’t think it’s BASH or Mac.

EDIT:

I wrote a basic C program and I get

~/workspace/x-cli :) ./a.out | GREP 'X_'
X_USERNAME=wooot
X_REPOSITORY=wooot

which correctly corresponds with printenv statement above. This is the code we are talking about (and this also affects Getenv and LookupEnv).

for _, e := range os.Environ() {
	fmt.Println(e)
}

and

if username, ok := os.LookupEnv("X_USERNAME"); ok {
	Username = username
}
if password, ok := os.LookupEnv("X_PASSWORD"); ok {
	Password = password
}
// ...

I found a bug in my solution. Nevermind. Argparse library returns a pointer to a string, which is never nil, but it can be empty. :man_shrugging:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.