[Go] Read an environment variable

Created on November 30, 2026

Use os.Getenv to read an environment variable by name. It returns an empty string if the variable is unset. Save the example as main.go.

package main

import(
    "fmt"
    "os"
)

func main() {
    fmt.Println(os.Getenv("HOGE"))
}

Set HOGE when starting the process.

HOGE=hello go run main.go
hello
Go