Standard library / os

os.ExpandEnv()

Expands variable references from the process environment; undefined names become empty. No shell execution occurs. Example intended for Linux/AMD64 with Go 1.23.2 in a disposable working directory; other platforms may differ. Expected stdout: 'Hello Go\n'.

Formal Syntax & Structure

go syntaxgo
func ExpandEnv(s string) string

Parameters & Attributes

NameTypeDescription
sstringText containing variable references.

Return Value

string

Expands variable references from the process environment; undefined names become empty.

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", "Go"))  fmt.Println(os.ExpandEnv("Hello ${BYTEMIND_SAMPLE}"))}
Go Reference