How to set, get, and list environment variables?


Use os.Environ to list all key/values of the environment. os.Environ() returns a slice of strings in the form KEY=value. Then we use strings.Split to get the key and value. Then print all the keys and values.

Example

package main

import (
  "os"
  "fmt"  
  "strings"  
)

func main() {	
	// Set custom env variable
	os.Setenv("CUSTOM", "500")
	
	// fetcha all env variables
	for _, element := range os.Environ() {
        variable := strings.Split(element, "=")
        fmt.Println(variable[0],"=>",variable[1])		
    }
	
	// fetch specific env variables
	fmt.Println("CUSTOM=>", os.Getenv("CUSTOM"))
	fmt.Println("GOROOT=>", os.Getenv("GOROOT"))
}

Output

 => ::
 => C:
 => ExitCode
ALLUSERSPROFILE => C:\ProgramData
AMDAPPSDKROOT => C:\Program Files\AMD APP\
APPDATA => C:\Users\amit\AppData\Roaming
CommonProgramFiles => C:\Program Files\Common Files
COMPUTERNAME => ADMIN
ComSpec => C:\Windows\system32\cmd.exe
CUSTOM => 500
FP_NO_HOST_CHECK => NO
GOBIN => C:\Go\bin
GOPATH => C:\Goweb
GOROOT => C:\Go
HOMEDRIVE => C:
HOMEPATH => \Users\amit
LOCALAPPDATA => C:\Users\amit\AppData\Local
LOGONSERVER => \\ADMIN
NUMBER_OF_PROCESSORS => 1
OS => Windows_NT
Path => C:\Program Files\AMD APP\bin\x86;C:\ProgramData\Oracle\Java\javapath;C:\Program Files\PHP\v5.6;C:\Program Files\Mozilla Firefox;C:\Win
dows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft\Web Platform Installe
r\;C:\Program Files\MySQL\MySQL Server 5.5\bin;C:\Program Files\MySQL\MySQL Fabric 1.5.4 & MySQL Utilities 1.5.4 1.5\;C:\Program Files\MySQL\M
ySQL Fabric 1.5.4 & MySQL Utilities 1.5.4 1.5\Doctrine extensions for PHP\;C:\xampp\php;C:\ProgramData\ComposerSetup\bin;C:\Users\amit\Anacond
a3;C:\Users\amit\Anaconda3\Scripts;C:\Users\amit\Anaconda3\Library\bin;C:\Python34;C:\Go\bin;C:\Program Files\ATI Technologies\ATI.ACE\Core-St
atic;C:\Program Files\Git\cmd;C:\Program Files\Skype\Phone\;C:\Users\amit\AppData\Local\Programs\Python\Python35-32\Scripts\;C:\Users\amit\App
Data\Local\Programs\Python\Python35-32\;C:\Users\amit\Anaconda3;C:\Users\amit\Anaconda3\Scripts;C:\Users\amit\Anaconda3\Library\bin;C:\cygwin;
C:\Python34;C:\Python34\Scripts;C:\Python34\Scripts;C:\Program Files\mingw-w64\i686-7.1.0-posix-dwarf-rt_v5-rev2\mingw32\bin;
PATHEXT => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE => x86
PROCESSOR_IDENTIFIER => x86 Family 16 Model 6 Stepping 3, AuthenticAMD
PROCESSOR_LEVEL => 16
PROCESSOR_REVISION => 0603
ProgramData => C:\ProgramData
ProgramFiles => C:\Program Files
PROMPT => $P$G
PSModulePath => C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
PUBLIC => C:\Users\Public
SESSIONNAME => Console
SystemDrive => C:
SystemRoot => C:\Windows
TEMP => C:\Users\amit\AppData\Local\Temp
TMP => C:\Users\amit\AppData\Local\Temp
USERDOMAIN => admin
USERNAME => amit
USERPROFILE => C:\Users\amit
windir => C:\Windows
_JAVA_OPTIONS => -Djava.net.preferIPv4Stack
CUSTOM=> 500
GOROOT=> C:\Go
Most Helpful This Week