Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os: Getenv() don't work when it has more than 1 env inside #60867

Closed
Necoo33 opened this issue Jun 19, 2023 · 6 comments
Closed

os: Getenv() don't work when it has more than 1 env inside #60867

Necoo33 opened this issue Jun 19, 2023 · 6 comments
Labels
OS-Windows WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@Necoo33
Copy link

Necoo33 commented Jun 19, 2023

Hello, i'm using go v1.20.4 and os.Getenv function don't work when it has more than 1 variable inside it.

.env file looks like this:

LOCALADDR="http://localhost:2000"
WEBSITEADDR="http://www.postgres-with-go.com"
ENVIRONMENT="dev"

When i find that bug, i make some tests like this:

	_, exist := os.LookupEnv("LOCALADDR")
	_, exist1 := os.LookupEnv("WEBSITEADDR")
	_, exist2 := os.LookupEnv("ENVIRONMENT")

	fmt.Printf("localAddr: %v \nwebsiteAddr: %v \nenvironment: %v \n", exist, exist1, exist2)

And i take this Promt in the terminal:

localAddr: false
websiteAddr: false
environment: false

Normally, i should use environment variables but i don't know why i cant if they are more than 1. This also happened when i try to write the values without quotes. I'm using Windows 11 and Visual Studio Code.

Update: It's Solved

First, I'm solved it with giving env variables to empty string on .env file:

LOCALADDR=""
WEBSITEADDR=""
ENVIRONMENT=""

Later than that i use os.Setenv() functions for each variable:

	os.Setenv("LOCALADDR", "http://localhost:2000")
	os.Setenv("WEBSITEADDR", "http://postgres-with-go.com")
	os.Setenv("ENVIRONMENT", "dev")

	_, exist := os.LookupEnv("LOCALADDR")
	_, exist1 := os.LookupEnv("WEBSITEADDR")
	_, exist2 := os.LookupEnv("ENVIRONMENT")

	fmt.Printf("localAddr: %v \nwebsiteAddr: %v \nenvironment: %v \n", exist, exist1, exist2)

And i take this prompt on terminal:

localAddr: true
websiteAddr: true
environment: true

So, why this weird thing happened from beginning? Why i have to set this variables manually?

@ianlancetaylor
Copy link
Contributor

CC @golang/windows

@seankhliao
Copy link
Member

seankhliao commented Jun 19, 2023

have you checked that your shell has actually set the environment variables correctly?

@seankhliao seankhliao changed the title affected/package: os.Getenv() don't work when it has more than 1 env inside os: Getenv() don't work when it has more than 1 env inside Jun 19, 2023
@seankhliao seankhliao added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jun 19, 2023
@Necoo33
Copy link
Author

Necoo33 commented Jun 19, 2023

have you checked that your shell has actually set the environment variables correctly?

I don't have any idea about what you said. If you mean in .env file there is no empty space right of the lines i checked it.

@seankhliao
Copy link
Member

how does a line in your .env file get into your shell? Go doesn't read .env files. Did your shell pick up the your config? Which shell/platform combination is this running on? how is your program executed?

@Necoo33
Copy link
Author

Necoo33 commented Jun 19, 2023

how does a line in your .env file get into your shell? Go doesn't read .env files. Did your shell pick up the your config? Which shell/platform combination is this running on? how is your program executed?

I'm executed program with "go run main.go" command. I'm using Visual Studio Core Insiders, and i don't know anything about "shell".

@seankhliao
Copy link
Member

closing as I don't think this is a bug in Go, rather it is in whatever thing that sources the lines from your file into the environment.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jun 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-Windows WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants