0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
overexchange
  • 15,768
  • 30
  • 152
  • 347
  • Is GoLang a requirement or do you just need to set environment variables? – MC10 Apr 03 '19 at 16:53
  • @MC10 GoLang is suppose to read the values of environment variables. This is the requirement – overexchange Apr 03 '19 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. – Adam Smith Apr 03 '19 at 17:27

4 Answers4

6

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
sh.seo
  • 1,482
  • 13
  • 20
  • 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. – Eryk Sun Apr 03 '19 at 22:08
  • 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". – Eryk Sun Apr 03 '19 at 22:09
  • 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. – Eryk Sun Apr 03 '19 at 22:14
  • 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". – Jeffrey Van Alstine Nov 17 '21 at 22:04
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%;
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
  • Please review *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (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](https://stackoverflow.com/posts/66840101/edit) (it covers answers and most kind of text as well). Thanks in advance. – Peter Mortensen Dec 31 '22 at 03:18
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"))
}
CV_Rookie
  • 19
  • 3
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.

ad absurdum
  • 19,498
  • 5
  • 37
  • 60
devocip
  • 11