Standard library / os
os.Getenv()
Returns the value of an environment variable or an empty string if absent. Use LookupEnv to distinguish absent from empty. Example intended for Linux/AMD64 with Go 1.23.2 in a disposable working directory; other platforms may differ. Expected stdout: 'local\n'.
Formal Syntax & Structure
go syntaxgo
func Getenv(key string) stringParameters & Attributes
| Name | Type | Description |
|---|---|---|
| key | string | Environment variable name. |
Return Value
string
Returns the value of an environment variable or an empty string if absent.
Working Example
Example Snippetgo
package main
import ( "fmt" "os")
func must(e error) { if e != nil { panic(e) }}func main() { must(os.Setenv("BYTEMIND_SAMPLE", "local")); fmt.Println(os.Getenv("BYTEMIND_SAMPLE")) }