os

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 8, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// SliceSeparator is the delimiter used to separate elements in a slice when parsing comma-separated values.
	SliceSeparator = ","
	// MapSeparator is the delimiter used to separate key-value pairs within a string representing a map.
	MapSeparator = ","
	// MapKVSeparator is the delimiter used to separate key and value in a key-value pair.
	MapKVSeparator = ":"
)

Functions

func Expand added in v0.11.0

func Expand(s string) (string, error)

func Getenv

func Getenv(key string, def string) string

Getenv wraps os.Getenv and returns the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")

	fmt.Println(osx.Getenv("FOO", "fallback"))

	_ = os.Setenv("FOO", "bar")

	fmt.Println(osx.Getenv("FOO", "fallback"))

}
Output:
fallback
bar

func GetenvBool

func GetenvBool(key string, def bool) (bool, error)

GetenvBool wraps os.Getenv and returns a bool or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")
	v, _ := osx.GetenvBool("FOO", false)
	fmt.Println(v)

	_ = os.Setenv("FOO", "true")
	v, _ = osx.GetenvBool("FOO", false)
	fmt.Println(v)

}
Output:
false
true

func GetenvBoolMap added in v0.6.0

func GetenvBoolMap(key string, def map[string]bool) (map[string]bool, error)

GetenvBoolMap wraps os.Getenv and returns a map[string]bool or the given default if empty or not defined. The expected format is comma-separated key:value pairs (e.g. "debug:true,verbose:false").

Example
package main

import (
	"fmt"
	"os"
	"sort"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "debug:true, verbose:false")
	v, _ := osx.GetenvBoolMap("FOO", nil)

	keys := make([]string, 0, len(v))
	for k := range v {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	for _, k := range keys {
		fmt.Printf("%s:%v\n", k, v[k])
	}

}
Output:
debug:true
verbose:false

func GetenvBoolSlice added in v0.6.0

func GetenvBoolSlice(key string, def []bool) ([]bool, error)

GetenvBoolSlice wraps os.Getenv and returns a bool slice or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")
	v, _ := osx.GetenvBoolSlice("FOO", nil)
	fmt.Println(v)

	_ = os.Setenv("FOO", "true,false,true")
	v, _ = osx.GetenvBoolSlice("FOO", nil)
	fmt.Println(v)

}
Output:
[]
[true false true]

func GetenvDuration added in v0.6.0

func GetenvDuration(key string, def time.Duration) (time.Duration, error)

GetenvDuration wraps os.Getenv and returns a time.Duration or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"
	"time"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")
	v, _ := osx.GetenvDuration("FOO", 5*time.Second)
	fmt.Println(v)

	_ = os.Setenv("FOO", "100ms")
	v, _ = osx.GetenvDuration("FOO", 5*time.Second)
	fmt.Println(v)

	_ = os.Setenv("FOO", "invalid")
	_, err := osx.GetenvDuration("FOO", 0)
	fmt.Println(err != nil)

}
Output:
5s
100ms
true

func GetenvDurationMap added in v0.6.0

func GetenvDurationMap(key string, def map[string]time.Duration) (map[string]time.Duration, error)

GetenvDurationMap wraps os.Getenv and returns a map[string]time.Duration or the given default if empty or not defined. The expected format is comma-separated key:value pairs (e.g. "timeout:5s,interval:100ms").

Example
package main

import (
	"fmt"
	"os"
	"sort"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "timeout:5s, interval:100ms")
	v, _ := osx.GetenvDurationMap("FOO", nil)

	keys := make([]string, 0, len(v))
	for k := range v {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	for _, k := range keys {
		fmt.Printf("%s:%v\n", k, v[k])
	}

}
Output:
interval:100ms
timeout:5s

func GetenvDurationSlice added in v0.6.0

func GetenvDurationSlice(key string, def []time.Duration) ([]time.Duration, error)

GetenvDurationSlice wraps os.Getenv and returns a time.Duration slice or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "1s, 500ms, 2m")
	v, _ := osx.GetenvDurationSlice("FOO", nil)
	fmt.Println(v)

}
Output:
[1s 500ms 2m0s]

func GetenvFloat32 added in v0.2.0

func GetenvFloat32(key string, def float32) (float32, error)

GetenvFloat32 wraps os.Getenv and returns a float32 or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")
	v, _ := osx.GetenvFloat32("FOO", 0.5)
	fmt.Println(v)

	_ = os.Setenv("FOO", "1.5")
	v, _ = osx.GetenvFloat32("FOO", 0.5)
	fmt.Println(v)

	_ = os.Setenv("FOO", "not-a-number")
	_, err := osx.GetenvFloat32("FOO", 0)
	fmt.Println(err != nil)

}
Output:
0.5
1.5
true

func GetenvFloat32Map added in v0.6.0

func GetenvFloat32Map(key string, def map[string]float32) (map[string]float32, error)

GetenvFloat32Map wraps os.Getenv and returns a map[string]float32 or the given default if empty or not defined. The expected format is comma-separated key:value pairs (e.g. "a:1.5,b:2.5").

Example
package main

import (
	"fmt"
	"os"
	"sort"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "a:1.5, b:2.5")
	v, _ := osx.GetenvFloat32Map("FOO", nil)

	keys := make([]string, 0, len(v))
	for k := range v {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	for _, k := range keys {
		fmt.Printf("%s:%.1f\n", k, v[k])
	}

}
Output:
a:1.5
b:2.5

func GetenvFloat32Slice added in v0.6.0

func GetenvFloat32Slice(key string, def []float32) ([]float32, error)

GetenvFloat32Slice wraps os.Getenv and returns a float32 slice or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "1.1, 2.2, 3.3")
	v, _ := osx.GetenvFloat32Slice("FOO", nil)
	fmt.Println(v)

}
Output:
[1.1 2.2 3.3]

func GetenvFloat64 added in v0.0.2

func GetenvFloat64(key string, def float64) (float64, error)

GetenvFloat64 wraps os.Getenv and returns a float64 or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")
	v, _ := osx.GetenvFloat64("FOO", 0.1)
	fmt.Println(v)

	_ = os.Setenv("FOO", "0.2")
	v, _ = osx.GetenvFloat64("FOO", 0.1)
	fmt.Println(v)

}
Output:
0.1
0.2

func GetenvFloat64Map added in v0.6.0

func GetenvFloat64Map(key string, def map[string]float64) (map[string]float64, error)

GetenvFloat64Map wraps os.Getenv and returns a map[string]float64 or the given default if empty or not defined. The expected format is comma-separated key:value pairs (e.g. "a:1.5,b:2.5").

Example
package main

import (
	"fmt"
	"os"
	"sort"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "a:1.5, b:2.5")
	v, _ := osx.GetenvFloat64Map("FOO", nil)

	keys := make([]string, 0, len(v))
	for k := range v {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	for _, k := range keys {
		fmt.Printf("%s:%.1f\n", k, v[k])
	}

}
Output:
a:1.5
b:2.5

func GetenvFloat64Slice added in v0.6.0

func GetenvFloat64Slice(key string, def []float64) ([]float64, error)

GetenvFloat64Slice wraps os.Getenv and returns a float64 slice or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "1.1, 2.2, 3.3")
	v, _ := osx.GetenvFloat64Slice("FOO", nil)
	fmt.Println(v)

}
Output:
[1.1 2.2 3.3]

func GetenvInt

func GetenvInt(key string, def int) (int, error)

GetenvInt wraps os.Getenv and returns an int or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")
	v, _ := osx.GetenvInt("FOO", 1)
	fmt.Println(v)

	_ = os.Setenv("FOO", "42")
	v, _ = osx.GetenvInt("FOO", 1)
	fmt.Println(v)

}
Output:
1
42

func GetenvInt8 added in v0.6.0

func GetenvInt8(key string, def int8) (int8, error)

GetenvInt8 wraps os.Getenv and returns an int8 or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")
	v, _ := osx.GetenvInt8("FOO", 1)
	fmt.Println(v)

	_ = os.Setenv("FOO", "127")
	v, _ = osx.GetenvInt8("FOO", 1)
	fmt.Println(v)

	_ = os.Setenv("FOO", "128")
	_, err := osx.GetenvInt8("FOO", 0)
	fmt.Println(err != nil)

}
Output:
1
127
true

func GetenvInt8Map added in v0.6.0

func GetenvInt8Map(key string, def map[string]int8) (map[string]int8, error)

GetenvInt8Map wraps os.Getenv and returns a map[string]int8 or the given default if empty or not defined. The expected format is comma-separated key:value pairs (e.g. "a:1,b:2").

Example
package main

import (
	"fmt"
	"os"
	"sort"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "a:1, b:2")
	v, _ := osx.GetenvInt8Map("FOO", nil)

	keys := make([]string, 0, len(v))
	for k := range v {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	for _, k := range keys {
		fmt.Printf("%s:%d\n", k, v[k])
	}

}
Output:
a:1
b:2

func GetenvInt8Slice added in v0.6.0

func GetenvInt8Slice(key string, def []int8) ([]int8, error)

GetenvInt8Slice wraps os.Getenv and returns an int8 slice or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "1, 2, 3")
	v, _ := osx.GetenvInt8Slice("FOO", nil)
	fmt.Println(v)

}
Output:
[1 2 3]

func GetenvInt16 added in v0.6.0

func GetenvInt16(key string, def int16) (int16, error)

GetenvInt16 wraps os.Getenv and returns an int16 or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")
	v, _ := osx.GetenvInt16("FOO", 1)
	fmt.Println(v)

	_ = os.Setenv("FOO", "1000")
	v, _ = osx.GetenvInt16("FOO", 1)
	fmt.Println(v)

}
Output:
1
1000

func GetenvInt16Map added in v0.6.0

func GetenvInt16Map(key string, def map[string]int16) (map[string]int16, error)

GetenvInt16Map wraps os.Getenv and returns a map[string]int16 or the given default if empty or not defined. The expected format is comma-separated key:value pairs (e.g. "a:1,b:2").

Example
package main

import (
	"fmt"
	"os"
	"sort"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "a:100, b:200")
	v, _ := osx.GetenvInt16Map("FOO", nil)

	keys := make([]string, 0, len(v))
	for k := range v {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	for _, k := range keys {
		fmt.Printf("%s:%d\n", k, v[k])
	}

}
Output:
a:100
b:200

func GetenvInt16Slice added in v0.6.0

func GetenvInt16Slice(key string, def []int16) ([]int16, error)

GetenvInt16Slice wraps os.Getenv and returns an int16 slice or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "100, 200, 300")
	v, _ := osx.GetenvInt16Slice("FOO", nil)
	fmt.Println(v)

}
Output:
[100 200 300]

func GetenvInt32 added in v0.2.0

func GetenvInt32(key string, def int32) (int32, error)

GetenvInt32 wraps os.Getenv and returns an int32 or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")
	v, _ := osx.GetenvInt32("FOO", 1)
	fmt.Println(v)

	_ = os.Setenv("FOO", "2")
	v, _ = osx.GetenvInt32("FOO", 1)
	fmt.Println(v)

	_ = os.Setenv("FOO", "0x1F")
	v, _ = osx.GetenvInt32("FOO", 0)
	fmt.Println(v)

	_ = os.Setenv("FOO", "2147483648")
	_, err := osx.GetenvInt32("FOO", 0)
	fmt.Println(err != nil)

}
Output:
1
2
31
true

func GetenvInt32Map added in v0.6.0

func GetenvInt32Map(key string, def map[string]int32) (map[string]int32, error)

GetenvInt32Map wraps os.Getenv and returns a map[string]int32 or the given default if empty or not defined. The expected format is comma-separated key:value pairs (e.g. "a:1,b:2").

Example
package main

import (
	"fmt"
	"os"
	"sort"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "a:100, b:200")
	v, _ := osx.GetenvInt32Map("FOO", nil)

	keys := make([]string, 0, len(v))
	for k := range v {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	for _, k := range keys {
		fmt.Printf("%s:%d\n", k, v[k])
	}

}
Output:
a:100
b:200

func GetenvInt32Slice added in v0.6.0

func GetenvInt32Slice(key string, def []int32) ([]int32, error)

GetenvInt32Slice wraps os.Getenv and returns an int32 slice or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "100, 200, 300")
	v, _ := osx.GetenvInt32Slice("FOO", nil)
	fmt.Println(v)

}
Output:
[100 200 300]

func GetenvInt64 added in v0.0.2

func GetenvInt64(key string, def int64) (int64, error)

GetenvInt64 wraps os.Getenv and returns an int64 or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")
	v, _ := osx.GetenvInt64("FOO", 1)
	fmt.Println(v)

	_ = os.Setenv("FOO", "2")
	v, _ = osx.GetenvInt64("FOO", 1)
	fmt.Println(v)

}
Output:
1
2

func GetenvInt64Map added in v0.6.0

func GetenvInt64Map(key string, def map[string]int64) (map[string]int64, error)

GetenvInt64Map wraps os.Getenv and returns a map[string]int64 or the given default if empty or not defined. The expected format is comma-separated key:value pairs (e.g. "a:1,b:2").

Example
package main

import (
	"fmt"
	"os"
	"sort"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "a:100, b:200")
	v, _ := osx.GetenvInt64Map("FOO", nil)

	keys := make([]string, 0, len(v))
	for k := range v {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	for _, k := range keys {
		fmt.Printf("%s:%d\n", k, v[k])
	}

}
Output:
a:100
b:200

func GetenvInt64Slice added in v0.6.0

func GetenvInt64Slice(key string, def []int64) ([]int64, error)

GetenvInt64Slice wraps os.Getenv and returns an int64 slice or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "100, 200, 300")
	v, _ := osx.GetenvInt64Slice("FOO", nil)
	fmt.Println(v)

}
Output:
[100 200 300]

func GetenvIntMap added in v0.6.0

func GetenvIntMap(key string, def map[string]int) (map[string]int, error)

GetenvIntMap wraps os.Getenv and returns a map[string]int or the given default if empty or not defined. The expected format is comma-separated key:value pairs (e.g. "a:1,b:2").

Example
package main

import (
	"fmt"
	"os"
	"sort"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "a:1, b:2")
	v, _ := osx.GetenvIntMap("FOO", nil)

	keys := make([]string, 0, len(v))
	for k := range v {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	for _, k := range keys {
		fmt.Printf("%s:%d\n", k, v[k])
	}

}
Output:
a:1
b:2

func GetenvIntSlice added in v0.6.0

func GetenvIntSlice(key string, def []int) ([]int, error)

GetenvIntSlice wraps os.Getenv and returns an int slice or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")
	v, _ := osx.GetenvIntSlice("FOO", nil)
	fmt.Println(v)

	_ = os.Setenv("FOO", "1, 2, 3")
	v, _ = osx.GetenvIntSlice("FOO", nil)
	fmt.Println(v)

}
Output:
[]
[1 2 3]

func GetenvStringMap added in v0.6.0

func GetenvStringMap(key string, def map[string]string) (map[string]string, error)

GetenvStringMap wraps os.Getenv and returns a map[string]string or the given default if empty or not defined. The expected format is comma-separated key:value pairs (e.g. "a:1,b:2").

Example
package main

import (
	"fmt"
	"os"
	"sort"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "a:1")
	v, _ := osx.GetenvStringMap("FOO", nil)
	fmt.Println(v)

	_ = os.Setenv("FOO", " x : hello , y : world ")
	v, _ = osx.GetenvStringMap("FOO", nil)

	keys := make([]string, 0, len(v))
	for k := range v {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	for _, k := range keys {
		fmt.Printf("%s:%s\n", k, v[k])
	}

	_ = os.Setenv("FOO", "invalid")
	_, err := osx.GetenvStringMap("FOO", nil)
	fmt.Println(err != nil)

}
Output:
map[a:1]
x:hello
y:world
true

func GetenvStringMapString deprecated added in v0.2.0

func GetenvStringMapString(key string, def map[string]string) (map[string]string, error)

Deprecated: Use GetenvStringMap instead.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "a:1")
	v, _ := osx.GetenvStringMapString("FOO", nil)
	fmt.Println(v)

}
Output:
map[a:1]

func GetenvStringSlice added in v0.0.2

func GetenvStringSlice(key string, def []string) []string

GetenvStringSlice wraps os.Getenv and returns a string slice or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")

	fmt.Println(osx.GetenvStringSlice("FOO", nil))

	_ = os.Setenv("FOO", "foo")

	fmt.Println(osx.GetenvStringSlice("FOO", nil))

	_ = os.Setenv("FOO", "foo,bar")

	fmt.Println(osx.GetenvStringSlice("FOO", nil))

}
Output:
[]
[foo]
[foo bar]

func GetenvUint added in v0.6.0

func GetenvUint(key string, def uint) (uint, error)

GetenvUint wraps os.Getenv and returns a uint or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")
	v, _ := osx.GetenvUint("FOO", 1)
	fmt.Println(v)

	_ = os.Setenv("FOO", "42")
	v, _ = osx.GetenvUint("FOO", 1)
	fmt.Println(v)

}
Output:
1
42

func GetenvUint8 added in v0.6.0

func GetenvUint8(key string, def uint8) (uint8, error)

GetenvUint8 wraps os.Getenv and returns a uint8 or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")
	v, _ := osx.GetenvUint8("FOO", 1)
	fmt.Println(v)

	_ = os.Setenv("FOO", "255")
	v, _ = osx.GetenvUint8("FOO", 1)
	fmt.Println(v)

	_ = os.Setenv("FOO", "256")
	_, err := osx.GetenvUint8("FOO", 0)
	fmt.Println(err != nil)

}
Output:
1
255
true

func GetenvUint8Map added in v0.6.0

func GetenvUint8Map(key string, def map[string]uint8) (map[string]uint8, error)

GetenvUint8Map wraps os.Getenv and returns a map[string]uint8 or the given default if empty or not defined. The expected format is comma-separated key:value pairs (e.g. "a:1,b:2").

Example
package main

import (
	"fmt"
	"os"
	"sort"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "a:1, b:2")
	v, _ := osx.GetenvUint8Map("FOO", nil)

	keys := make([]string, 0, len(v))
	for k := range v {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	for _, k := range keys {
		fmt.Printf("%s:%d\n", k, v[k])
	}

}
Output:
a:1
b:2

func GetenvUint8Slice added in v0.6.0

func GetenvUint8Slice(key string, def []uint8) ([]uint8, error)

GetenvUint8Slice wraps os.Getenv and returns a uint8 slice or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "1, 2, 3")
	v, _ := osx.GetenvUint8Slice("FOO", nil)
	fmt.Println(v) //nolint:staticcheck // QF1010

}
Output:
[1 2 3]

func GetenvUint16 added in v0.6.0

func GetenvUint16(key string, def uint16) (uint16, error)

GetenvUint16 wraps os.Getenv and returns a uint16 or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")
	v, _ := osx.GetenvUint16("FOO", 1)
	fmt.Println(v)

	_ = os.Setenv("FOO", "65535")
	v, _ = osx.GetenvUint16("FOO", 1)
	fmt.Println(v)

}
Output:
1
65535

func GetenvUint16Map added in v0.6.0

func GetenvUint16Map(key string, def map[string]uint16) (map[string]uint16, error)

GetenvUint16Map wraps os.Getenv and returns a map[string]uint16 or the given default if empty or not defined. The expected format is comma-separated key:value pairs (e.g. "a:1,b:2").

Example
package main

import (
	"fmt"
	"os"
	"sort"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "a:1, b:2")
	v, _ := osx.GetenvUint16Map("FOO", nil)

	keys := make([]string, 0, len(v))
	for k := range v {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	for _, k := range keys {
		fmt.Printf("%s:%d\n", k, v[k])
	}

}
Output:
a:1
b:2

func GetenvUint16Slice added in v0.6.0

func GetenvUint16Slice(key string, def []uint16) ([]uint16, error)

GetenvUint16Slice wraps os.Getenv and returns a uint16 slice or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "1, 2, 3")
	v, _ := osx.GetenvUint16Slice("FOO", nil)
	fmt.Println(v)

}
Output:
[1 2 3]

func GetenvUint32 added in v0.6.0

func GetenvUint32(key string, def uint32) (uint32, error)

GetenvUint32 wraps os.Getenv and returns a uint32 or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")
	v, _ := osx.GetenvUint32("FOO", 1)
	fmt.Println(v)

	_ = os.Setenv("FOO", "100000")
	v, _ = osx.GetenvUint32("FOO", 1)
	fmt.Println(v)

}
Output:
1
100000

func GetenvUint32Map added in v0.6.0

func GetenvUint32Map(key string, def map[string]uint32) (map[string]uint32, error)

GetenvUint32Map wraps os.Getenv and returns a map[string]uint32 or the given default if empty or not defined. The expected format is comma-separated key:value pairs (e.g. "a:1,b:2").

Example
package main

import (
	"fmt"
	"os"
	"sort"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "a:1, b:2")
	v, _ := osx.GetenvUint32Map("FOO", nil)

	keys := make([]string, 0, len(v))
	for k := range v {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	for _, k := range keys {
		fmt.Printf("%s:%d\n", k, v[k])
	}

}
Output:
a:1
b:2

func GetenvUint32Slice added in v0.6.0

func GetenvUint32Slice(key string, def []uint32) ([]uint32, error)

GetenvUint32Slice wraps os.Getenv and returns a uint32 slice or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "1, 2, 3")
	v, _ := osx.GetenvUint32Slice("FOO", nil)
	fmt.Println(v)

}
Output:
[1 2 3]

func GetenvUint64 added in v0.6.0

func GetenvUint64(key string, def uint64) (uint64, error)

GetenvUint64 wraps os.Getenv and returns a uint64 or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "")
	v, _ := osx.GetenvUint64("FOO", 1)
	fmt.Println(v)

	_ = os.Setenv("FOO", "18446744073709551615")
	v, _ = osx.GetenvUint64("FOO", 1)
	fmt.Println(v)

}
Output:
1
18446744073709551615

func GetenvUint64Map added in v0.6.0

func GetenvUint64Map(key string, def map[string]uint64) (map[string]uint64, error)

GetenvUint64Map wraps os.Getenv and returns a map[string]uint64 or the given default if empty or not defined. The expected format is comma-separated key:value pairs (e.g. "a:1,b:2").

Example
package main

import (
	"fmt"
	"os"
	"sort"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "a:1, b:2")
	v, _ := osx.GetenvUint64Map("FOO", nil)

	keys := make([]string, 0, len(v))
	for k := range v {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	for _, k := range keys {
		fmt.Printf("%s:%d\n", k, v[k])
	}

}
Output:
a:1
b:2

func GetenvUint64Slice added in v0.6.0

func GetenvUint64Slice(key string, def []uint64) ([]uint64, error)

GetenvUint64Slice wraps os.Getenv and returns a uint64 slice or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "1, 2, 3")
	v, _ := osx.GetenvUint64Slice("FOO", nil)
	fmt.Println(v)

}
Output:
[1 2 3]

func GetenvUintMap added in v0.6.0

func GetenvUintMap(key string, def map[string]uint) (map[string]uint, error)

GetenvUintMap wraps os.Getenv and returns a map[string]uint or the given default if empty or not defined. The expected format is comma-separated key:value pairs (e.g. "a:1,b:2").

Example
package main

import (
	"fmt"
	"os"
	"sort"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "a:1, b:2")
	v, _ := osx.GetenvUintMap("FOO", nil)

	keys := make([]string, 0, len(v))
	for k := range v {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	for _, k := range keys {
		fmt.Printf("%s:%d\n", k, v[k])
	}

}
Output:
a:1
b:2

func GetenvUintSlice added in v0.6.0

func GetenvUintSlice(key string, def []uint) ([]uint, error)

GetenvUintSlice wraps os.Getenv and returns a uint slice or the given default if empty or not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "1, 2, 3")
	v, _ := osx.GetenvUintSlice("FOO", nil)
	fmt.Println(v)

}
Output:
[1 2 3]

func HasEnv added in v0.0.2

func HasEnv(key string) bool

HasEnv return true if given env var is defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Unsetenv("FOO")

	fmt.Println(osx.HasEnv("FOO"))

	_ = os.Setenv("FOO", "bar")

	fmt.Println(osx.HasEnv("FOO"))

}
Output:
false
true

func MustGetenv added in v0.6.0

func MustGetenv(key string) string

MustGetenv returns the value of the environment variable or panics if it is not defined.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "bar")

	fmt.Println(osx.MustGetenv("FOO"))

}
Output:
bar

func MustGetenvBool added in v0.6.0

func MustGetenvBool(key string) bool

MustGetenvBool returns the bool value of the environment variable or panics if it is not defined or cannot be parsed.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "true")

	fmt.Println(osx.MustGetenvBool("FOO"))

}
Output:
true

func MustGetenvDuration added in v0.6.0

func MustGetenvDuration(key string) time.Duration

MustGetenvDuration returns the time.Duration value of the environment variable or panics if it is not defined or cannot be parsed.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "5s")

	fmt.Println(osx.MustGetenvDuration("FOO"))

}
Output:
5s

func MustGetenvFloat32 added in v0.6.0

func MustGetenvFloat32(key string) float32

MustGetenvFloat32 returns the float32 value of the environment variable or panics if it is not defined or cannot be parsed.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "1.5")

	fmt.Println(osx.MustGetenvFloat32("FOO"))

}
Output:
1.5

func MustGetenvFloat64 added in v0.6.0

func MustGetenvFloat64(key string) float64

MustGetenvFloat64 returns the float64 value of the environment variable or panics if it is not defined or cannot be parsed.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "3.14")

	fmt.Println(osx.MustGetenvFloat64("FOO"))

}
Output:
3.14

func MustGetenvInt added in v0.6.0

func MustGetenvInt(key string) int

MustGetenvInt returns the int value of the environment variable or panics if it is not defined or cannot be parsed.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "42")

	fmt.Println(osx.MustGetenvInt("FOO"))

}
Output:
42

func MustGetenvInt8 added in v0.6.0

func MustGetenvInt8(key string) int8

MustGetenvInt8 returns the int8 value of the environment variable or panics if it is not defined or cannot be parsed.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "127")

	fmt.Println(osx.MustGetenvInt8("FOO"))

}
Output:
127

func MustGetenvInt16 added in v0.6.0

func MustGetenvInt16(key string) int16

MustGetenvInt16 returns the int16 value of the environment variable or panics if it is not defined or cannot be parsed.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "1000")

	fmt.Println(osx.MustGetenvInt16("FOO"))

}
Output:
1000

func MustGetenvInt32 added in v0.6.0

func MustGetenvInt32(key string) int32

MustGetenvInt32 returns the int32 value of the environment variable or panics if it is not defined or cannot be parsed.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "100000")

	fmt.Println(osx.MustGetenvInt32("FOO"))

}
Output:
100000

func MustGetenvInt64 added in v0.6.0

func MustGetenvInt64(key string) int64

MustGetenvInt64 returns the int64 value of the environment variable or panics if it is not defined or cannot be parsed.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "100000")

	fmt.Println(osx.MustGetenvInt64("FOO"))

}
Output:
100000

func MustGetenvUint added in v0.6.0

func MustGetenvUint(key string) uint

MustGetenvUint returns the uint value of the environment variable or panics if it is not defined or cannot be parsed.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "42")

	fmt.Println(osx.MustGetenvUint("FOO"))

}
Output:
42

func MustGetenvUint8 added in v0.6.0

func MustGetenvUint8(key string) uint8

MustGetenvUint8 returns the uint8 value of the environment variable or panics if it is not defined or cannot be parsed.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "255")

	fmt.Println(osx.MustGetenvUint8("FOO"))

}
Output:
255

func MustGetenvUint16 added in v0.6.0

func MustGetenvUint16(key string) uint16

MustGetenvUint16 returns the uint16 value of the environment variable or panics if it is not defined or cannot be parsed.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "65535")

	fmt.Println(osx.MustGetenvUint16("FOO"))

}
Output:
65535

func MustGetenvUint32 added in v0.6.0

func MustGetenvUint32(key string) uint32

MustGetenvUint32 returns the uint32 value of the environment variable or panics if it is not defined or cannot be parsed.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "100000")

	fmt.Println(osx.MustGetenvUint32("FOO"))

}
Output:
100000

func MustGetenvUint64 added in v0.6.0

func MustGetenvUint64(key string) uint64

MustGetenvUint64 returns the uint64 value of the environment variable or panics if it is not defined or cannot be parsed.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "18446744073709551615")

	fmt.Println(osx.MustGetenvUint64("FOO"))

}
Output:
18446744073709551615

func MustHasEnv added in v0.0.2

func MustHasEnv(key string)

MustHasEnv panics if the given env var does not exists.

Example
package main

import (
	"fmt"
	"os"

	osx "github.com/foomo/go/os"
)

func main() {
	_ = os.Setenv("FOO", "bar")

	osx.MustHasEnv("FOO") // does not panic
	fmt.Println("ok")

}
Output:
ok

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL