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")
}
Feedback







Small world. Big idea!
  • Welcome to visit the knowledge base of SRE and DevOps!
  • License under CC BY-NC 4.0
  • Made with Material for MkDocs and improve writing by generative AI tools
  • Copyright issue feedback me#imzye.com, replace # with @