Skip to content

Read and Set Environment variables in Go

homepage-banner

Introduction

Environment variables are a way to store configuration information in an operating system. These variables are accessible to all the programs running on the operating system and can be used to configure or customize the behavior of these programs. In this blog post, we will discuss how to read and set environment variables in Go.

Here is a simple example to read and set environment variables in your go project.

package main

import (
    "fmt"
    "os"
)

func main() {
    // Read
    fmt.Println(os.Getenv("GOPATH"))

    // Set
    os.Setenv("GOPATH", "/home/username/go")
}
Leave a message