How do I permanently set environment variables in Windows for my Go code?

Linda Hamilton
Release: 2024-11-16 10:36:03
Original
267 people have browsed it

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
Copy after login

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
Copy after login

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"
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template