DevOps | Scripts | Automation

GoLang

How to Set Environment Variable using GoLang?

In the previous articles, we mentioned setting up environment variables using PowerShell and Python in Windows OS. In this article, we will set up environment variables with GoLang.

Set up environment variables with GoLang.

a. Process Level

Setting up environment variables at the process level is easy with GoLang. Below is an example of that.

package main

import (
	"fmt"
	"os"
)

func main(){
	env_key := "azSub"
	env_value := "TestSub1"

	err := os.Setenv(env_key,env_value)

	if err != nil {
		fmt.Println("Error Setting up process level environment variable", err)
		return
	}

	getEnvValue := os.Getenv(env_key)
	fmt.Println("Environment variable %s is set to: %s",env_key,getEnvValue)
}

In the above example, we must import the OS module from the main package and use the method setenv() to set the variable value.

Setting up value at the Machine level and user level requires registry changes and for that registry package for windows, golang.org/x/sys/windows/registry must be imported.

b. User-Level

Set the registry value Computer\HKEY_CURRENT_USER\Environment to set the environment variable at the user level. Here is the Go Program for that.

package main

import (
	"fmt"
	"golang.org/x/sys/windows/registry"
)

func setUserEnvVariable(variableName, variableValue string) {
	keyPath := `Environment`

	// Open the registry key for environment variables with write access
	key, err := registry.OpenKey(registry.CURRENT_USER, keyPath, registry.SET_VALUE)
	if err != nil {
		fmt.Println("Error opening registry key:", err)
		return
	}
	defer key.Close()

	// Set the environment variable in the registry
	err = key.SetStringValue(variableName, variableValue)
	if err != nil {
		fmt.Println("Error setting registry value:", err)
		return
	}

	fmt.Printf("User-level environment variable %s set to %s.\n", variableName, variableValue)
}

func main() {
	variableName := "azsub"
	variableValue := "TestSub4"

	setUserEnvVariable(variableName, variableValue)
}

c. Machine Level

To set the environment variable at the machine level, set the below registry key.

SYSTEM\CurrentControlSet\Control\Session Manager\Environment

Here is the program for that.

package main

import (
	"fmt"
	"golang.org/x/sys/windows/registry"
)

func setMachineEnvVariable(variableName, variableValue string) {
	keyPath := `SYSTEM\CurrentControlSet\Control\Session Manager\Environment`

	// Open the registry key for environment variables with write access
	key, err := registry.OpenKey(registry.LOCAL_MACHINE, keyPath, registry.SET_VALUE)
	if err != nil {
		fmt.Println("Error opening registry key:", err)
		return
	}
	defer key.Close()

	// Set the environment variable in the registry
	err = key.SetStringValue(variableName, variableValue)
	if err != nil {
		fmt.Println("Error setting registry value:", err)
		return
	}

	fmt.Printf("Machine-level environment variable %s set to %s.\n", variableName, variableValue)
}


func main() {
	variableName := "azSub"
	variableValue := "TestSub1"

	setMachineEnvVariable(variableName, variableValue)
}

The code is available on GitHub.

https://github.com/chiragce17/MyPublicRepo/tree/main/GoLang/Windows/EnvironmentVariables

Loading

One thought on “How to Set Environment Variable using GoLang?

Comments are closed.