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()


Clearenv deletes all environment variables.

Example with code:

package main

import (

func main() {

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

  name := os.Getenv("NAME")

  addr := os.Getenv("ADDR")

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


  name = os.Getenv("NAME")

  addr = os.Getenv("ADDR")

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



Name: Jon, Addr: USA

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

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

Posted in golang, os, packages

Leave a Reply

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