Skip to content

Read and Set Environment variables in Go



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 (

func main() {
    // Read

    // Set
    os.Setenv("GOPATH", "/home/username/go")

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, replace # with @