Menu Close

Go – How to delete all environment variables in go

Here, We will see how to delete all environment variables in go. We can do it by using Clearenv() function in os package in go golang.

Function prototype:

func Clearenv()

Usages:

Clearenv deletes all environment variables.

Example with code:

package main

import (
  "fmt"
  "os"
  "log"
)

func main() {

  err := os.Setenv("NAME", "Jon")
 
  if err != nil {
 
    log.Fatal(err)
 
  }
 
  err = os.Setenv("ADDR", "USA")
 
  if err != nil {
 
    log.Fatal(err)
 
  }

  name := os.Getenv("NAME")

  addr := os.Getenv("ADDR")

  fmt.Printf("Name: %s, Addr: %s\n", name, addr)

  os.Clearenv()

  name = os.Getenv("NAME")

  addr = os.Getenv("ADDR")

  fmt.Printf("\n Name: %s, Addr: %s\n", name, addr)

}

Output:

Name: Jon, Addr: USA

Name: , Addr:     [Here, It's empty]

To learn more about golang, Please refer given below link:

https://techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, os, packages

Leave a Reply

Your email address will not be published. Required fields are marked *