Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Set or Unset or Get Environment Variables in Golang

Posted on February 22, 2023February 22, 2023 by admin

Table of Contents

  • Overview
    • Setting an env value
    • Get an env value
    • Unset an env value
    • Lookup for an env variable
  • Code:

Overview

os package provides a couple of utility methods related to playing around env variables in Go.

Setting an env value

func Setenv(key, value string) error

Get an env value

The value returned by the Getenv() function will be empty if that env variable is not present. To differentiate between an unset and empty env variable use LookupEnv() function which an additional boolean indicating if the env variable was present or not.

func Getenv(key string) string

Unset an env value

func Unsetenv(key string) error 

Lookup for an env variable

As evident from the below signature, LookupEnv returns additional boolean.

  • If the environment variable represented by the key is present, then its value is returned and the bool returned is true.
  • If the environment variable represented by the key is not present, then the value is empty and the bool returned is false.
func LookupEnv(key string) (string, bool)

Code:

package main
import (
"fmt"
"log"
"os"
)
func main() {
//Set env a to b
err := os.Setenv("a", "b")
if err != nil {
log.Fatal(err)
}
//Get env a
val := os.Getenv("a")
fmt.Println(val)
//Unset a
err = os.Unsetenv("a")
if err != nil {
log.Fatal(err)
}
val, present := os.LookupEnv("a")
fmt.Printf("a env variable present: %t\n", present)
}

Output:

b
a env variable present: false

Popular Articles

Golang Comprehensive Tutorial Series

All Design Patterns in Go (Golang)

Slice in golang

Variables in Go (Golang) – Complete Guide

OOP: Inheritance in GOLANG complete guide

Using Context Package in GO (Golang) – Complete Guide

All data types in Golang with examples

Understanding time and date in Go (Golang) – Complete Guide

©2023 Welcome To Golang By Example | Design: Web XP