In Linux systems, examples of environment variables include the location of executable programs within the filesystem, the user shell, pwd, home directory for a specific user, motd, and many more.
Go, a modern and vast programming language provides the os package to interact with environment variables.
We will cover the fundamentals of working with environment variables in the Go language throughout this tutorial.
Getenv
The first method we are going to cover is the Getenv method. This method allows you to retrieve the values of an environment variable. The function takes the environment variable’s name as the parameter and returns its associated value.
If the environment variable specified does not exist on the system, the function returns an empty value. This may be a little confusing if the environment variable exists but is unset.
The function syntax is as shown:
Consider the example below:
import (
"fmt"
"os"
)
func main() {
fmt.Println("Home: ", os.Getenv("HOME"))
}
The example will retrieve the value of the home directory for the user. An example output is as shown:
Home: C:\Users\csalem // windows
Home: /home/debian // Debian
LookupEnv
As mentioned, the GetEnv function returns an empty value if the environment variable does not exist. However, the function will also return an empty value if the environment variable is not set.
To solve this, we can use the LookupEnv() function. The function will retrieve the value of the environment variable if it exists and return a Boolean true. If the environment variable does not exist, the function returns an empty value and a Boolean false.
Consider the example below:
_, found := os.LookupEnv(key)
if found {
fmt.Println("Environmet variable found")
} else {
fmt.Println("Environemnt variable not found")
}
}
The example holds a function that takes the environment variable as the parameter. We then use the value of the key to check if it exists using an if statement.
If we run the above code with two parameters as shown:
lookup("NOT_THERE")
The resulting output:
Environmet variable found
Environemnt variable not found
SetEnv
What if we want to set a value for a specific environment variable? In such a scenario, we can use the SetEnv() function. The function takes the key (name of the environment variable) and its associated value as the parameters.
The example below sets the value of the user shell:
fmt.Println("Before: ", os.Getenv("SHELL"))
os.Setenv("SHELL", "/bin/zsh")
fmt.Println("After: ", os.Getenv("SHELL"))
}
The above example sets the value of the SHELL environment variable to “/bin/zsh”. An example output is as:
After: /bin/zsh
UnsetEnv
We can also unset an environment variable using the UnsetEnv() method. An example is as shown:
fmt.Println("HOME: ", os.Getenv("HOME"))
os.Unsetenv("HOME")
fmt.Println("Home: ", os.Getenv("HOME"))
}
The example above will unset the value of the HOME env variable.
Output:
HOME: C:\Users\csalem // windows
Home:
Environ
The Environ() method allows you to list all the environment variables in a system. The function returns the result in the form of key-value pairs.
An example is shown below:
for _, env := range os.Environ() {
pair := strings.SplitN(env, "=", 2)
fmt.Printf("%s: %s\n", pair[0], pair[1])
}
}
The above example uses the Environ() method to list all the environment variables in the system. We then use the SplitN method to split the strings into key-value pairs.
Clearenv()
If you want to delete all the environment variables in your system, you can use the Clearenv() method.
Caution when using this method as it will reset all the env in the system.
To use it, consider the snippet below:
Expandenv
The Expandenv() allows you to replace the $var with the actual value of an environment variable inside a string. This is a very useful function when working with env.
For example:
fmt.Println(os.ExpandEnv("username: $USERNAME."))
}
The function will expand the “USERNAME” environment variable and substitute its value inside the string. The result is as:
Conclusion
This guide explored the foundation of working with environment variables in the go programming language using the os package.