1

Below is the Go code:

var ( Address = os.Getenv("ADDR") Token = os.Getenv("TOKEN") ) 

It reads the environment variables in Windows.


On a Windows laptop, I have the privilege to set user variables for my user login only. I created two variables (for the above), but os.Getenv() cannot read the values.

I do not have the privilege to set system variables.


How can I set environment variables in Windows, with my user login?

3
  • Is GoLang a requirement or do you just need to set environment variables? Commented Apr 3, 2019 at 16:53
  • @MC10 GoLang is suppose to read the values of environment variables. This is the requirement Commented Apr 3, 2019 at 16:55
  • 2
    If you've set the User environment variables, Golang can read them so long as you're running it as that user. Otherwise -- any user is allowed to set per-session env vars (SET key=value from the command line) so you could do that before script invocation. Commented Apr 3, 2019 at 17:27

4 Answers 4

8

In Windows, environment variables can be applied in two ways.

Set modifies the current shell's (the window's) environment values, and the change is available immediately, but it is temporary. The change will not affect other shells that are running, and as soon as you close the shell, the new value is lost until such time as you run set again.

cmd> SET ADDR=127.0.0.1 cmd> SET TOKEN=ABCD1234 cmd> SET 

setx modifies the value permanently, which affects all future shells, but does not modify the environment of the shells already running. You have to exit the shell and reopen it before the change will be available, but the value will remain modified until you change it again.

cmd> setx ADDR "127.0.0.1" cmd> setx TOKEN "ABCD1234" cmd> SET 
Sign up to request clarification or add additional context in comments.

4 Comments

The command-line shell (e.g. CMD or PowerShell) is not the console window. Its standard I/O can be directed anywhere or run without a window at all.
setx.exe modifies the registry and broadcasts a WM_SETTINGCHANGE message to top-level windows. This includes instances of the graphical shell Explorer, which in response reloads its environment from the registry. It does not include instances of CMD because it owns no windows, top-level or otherwise. New instances of CMD started from Explorer will inherit the updated environment. Instances started from other existing processes will not. It is not "all future shells".
An important disclaimer whenever we discuss setx.exe is that it shouldn't be used to modify PATH. In particular not in a naive manner that extends %PATH% and sets it back to the system or user registry value. It's possible if we're careful to read and modify the original system or user "Path" value in the registry via reg.exe. There are SO answers that show how to do this, but it's difficult and should probably just be avoided.
Does this not work in Powershell? The vanilla SET command just asks for parameters instead of listing env variables and if I SET ADDR="127.0.0.1" and then ECHO %ADDR% I get the literal string "%ADDR%", not the expected string "127.0.0.1".
1

You can try using the set method in a terminal in the following way:

set NODE_HOME=C:\Users\359855\Downloads\node-v14.16.0-win-x64 set PATH=%PATH%;%NODE_HOME%; 

1 Comment

Please review Why not upload images of code/errors when asking a question? (e.g., "Images should only be used to illustrate problems that can't be made clear in any other way, such as to provide screenshots of a user interface.") and take the appropriate action (it covers answers and most kind of text as well). Thanks in advance.
1

If you want to use some user-defined variable in Windows environment, I recommend the library godotenv

go get github.com/joho/godotenv 

you can set your variable in the .env file in the same directory of the main. go. like

ADDR=127.0.0.1 TOKEN=localhost 

In your go program, you can use these variable like:

package main import ( "fmt" "log" "os" "github.com/joho/godotenv" ) func main() { err := godotenv.Load() if err != nil { log.Fatal(err) } fmt.Println("ADDR: ", os.Getenv("ADDR")) fmt.Println("TOKEN: ", os.Getenv("TOKEN")) } 

Comments

0

I don't know if this is the same as yours, but I use: os.environ.get('environment variable').

Also you can add a print(environment variable) and run to check if everything is fine. Let's say the environment variable is SECRET_KEY. You add a print(SECRET_KEY) to your line of code and run, check your terminal for possible results.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.