Home >Backend Development >Golang >How do I permanently set environment variables in Windows for my Go code?
Setting Environment Variables in Windows
You have encountered an issue where you are unable to read environment variables in your Go code on a Windows laptop, despite having user privileges to set them. This is because there are two methods for setting environment variables in Windows:
1. Set Command:
The set command modifies the environment values for the current shell temporarily. The change is immediately available, but it affects only the current shell and will be lost when the shell is closed.
Example:
cmd> SET ADDR=127.0.0.1 cmd> SET TOKEN=ABCD1234 cmd> SET
2. Setx Command:
The setx command modifies the environment value permanently, affecting all future shells. However, it does not affect shells already running. You must exit and reopen the shell for the change to take effect.
Example:
cmd> setx ADDR "127.0.0.1" cmd> setx TOKEN "ABCD1234" cmd> SET
Since you only have user privileges, you can use the setx command to permanently set the environment variables for your user login:
setx ADDR "127.0.0.1" setx TOKEN "ABCD1234"
Once you have set the variables using setx, they will be available to your Go code through the os.Getenv() function.
The above is the detailed content of How do I permanently set environment variables in Windows for my Go code?. For more information, please follow other related articles on the PHP Chinese website!