Standard library / os

os.Setenv()

Sets a process-local environment variable. Children normally inherit the resulting environment unless overridden. Check errors for invalid keys. Example intended for Linux/AMD64 with Go 1.23.2 in a disposable working directory; other platforms may differ. Expected stdout: '42\n'.

Formal Syntax & Structure

go syntaxgo
func Setenv(key, value string) error

Parameters & Attributes

NameTypeDescription
keystringEnvironment variable name.
valuestringNew environment variable value.

Return Value

error

Sets a process-local environment variable.

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", "42")); fmt.Println(os.Getenv("BYTEMIND_SAMPLE")) }
Go Reference