Documentation
¶
Index ¶
- Variables
- func Expand(s string) (string, error)
- func Getenv(key string, def string) string
- func GetenvBool(key string, def bool) (bool, error)
- func GetenvBoolMap(key string, def map[string]bool) (map[string]bool, error)
- func GetenvBoolSlice(key string, def []bool) ([]bool, error)
- func GetenvDuration(key string, def time.Duration) (time.Duration, error)
- func GetenvDurationMap(key string, def map[string]time.Duration) (map[string]time.Duration, error)
- func GetenvDurationSlice(key string, def []time.Duration) ([]time.Duration, error)
- func GetenvFloat32(key string, def float32) (float32, error)
- func GetenvFloat32Map(key string, def map[string]float32) (map[string]float32, error)
- func GetenvFloat32Slice(key string, def []float32) ([]float32, error)
- func GetenvFloat64(key string, def float64) (float64, error)
- func GetenvFloat64Map(key string, def map[string]float64) (map[string]float64, error)
- func GetenvFloat64Slice(key string, def []float64) ([]float64, error)
- func GetenvInt(key string, def int) (int, error)
- func GetenvInt8(key string, def int8) (int8, error)
- func GetenvInt8Map(key string, def map[string]int8) (map[string]int8, error)
- func GetenvInt8Slice(key string, def []int8) ([]int8, error)
- func GetenvInt16(key string, def int16) (int16, error)
- func GetenvInt16Map(key string, def map[string]int16) (map[string]int16, error)
- func GetenvInt16Slice(key string, def []int16) ([]int16, error)
- func GetenvInt32(key string, def int32) (int32, error)
- func GetenvInt32Map(key string, def map[string]int32) (map[string]int32, error)
- func GetenvInt32Slice(key string, def []int32) ([]int32, error)
- func GetenvInt64(key string, def int64) (int64, error)
- func GetenvInt64Map(key string, def map[string]int64) (map[string]int64, error)
- func GetenvInt64Slice(key string, def []int64) ([]int64, error)
- func GetenvIntMap(key string, def map[string]int) (map[string]int, error)
- func GetenvIntSlice(key string, def []int) ([]int, error)
- func GetenvStringMap(key string, def map[string]string) (map[string]string, error)
- func GetenvStringMapString(key string, def map[string]string) (map[string]string, error)deprecated
- func GetenvStringSlice(key string, def []string) []string
- func GetenvUint(key string, def uint) (uint, error)
- func GetenvUint8(key string, def uint8) (uint8, error)
- func GetenvUint8Map(key string, def map[string]uint8) (map[string]uint8, error)
- func GetenvUint8Slice(key string, def []uint8) ([]uint8, error)
- func GetenvUint16(key string, def uint16) (uint16, error)
- func GetenvUint16Map(key string, def map[string]uint16) (map[string]uint16, error)
- func GetenvUint16Slice(key string, def []uint16) ([]uint16, error)
- func GetenvUint32(key string, def uint32) (uint32, error)
- func GetenvUint32Map(key string, def map[string]uint32) (map[string]uint32, error)
- func GetenvUint32Slice(key string, def []uint32) ([]uint32, error)
- func GetenvUint64(key string, def uint64) (uint64, error)
- func GetenvUint64Map(key string, def map[string]uint64) (map[string]uint64, error)
- func GetenvUint64Slice(key string, def []uint64) ([]uint64, error)
- func GetenvUintMap(key string, def map[string]uint) (map[string]uint, error)
- func GetenvUintSlice(key string, def []uint) ([]uint, error)
- func HasEnv(key string) bool
- func MustGetenv(key string) string
- func MustGetenvBool(key string) bool
- func MustGetenvDuration(key string) time.Duration
- func MustGetenvFloat32(key string) float32
- func MustGetenvFloat64(key string) float64
- func MustGetenvInt(key string) int
- func MustGetenvInt8(key string) int8
- func MustGetenvInt16(key string) int16
- func MustGetenvInt32(key string) int32
- func MustGetenvInt64(key string) int64
- func MustGetenvUint(key string) uint
- func MustGetenvUint8(key string) uint8
- func MustGetenvUint16(key string) uint16
- func MustGetenvUint32(key string) uint32
- func MustGetenvUint64(key string) uint64
- func MustHasEnv(key string)
Examples ¶
- Getenv
- GetenvBool
- GetenvBoolMap
- GetenvBoolSlice
- GetenvDuration
- GetenvDurationMap
- GetenvDurationSlice
- GetenvFloat32
- GetenvFloat32Map
- GetenvFloat32Slice
- GetenvFloat64
- GetenvFloat64Map
- GetenvFloat64Slice
- GetenvInt
- GetenvInt8
- GetenvInt8Map
- GetenvInt8Slice
- GetenvInt16
- GetenvInt16Map
- GetenvInt16Slice
- GetenvInt32
- GetenvInt32Map
- GetenvInt32Slice
- GetenvInt64
- GetenvInt64Map
- GetenvInt64Slice
- GetenvIntMap
- GetenvIntSlice
- GetenvStringMap
- GetenvStringMapString
- GetenvStringSlice
- GetenvUint
- GetenvUint8
- GetenvUint8Map
- GetenvUint8Slice
- GetenvUint16
- GetenvUint16Map
- GetenvUint16Slice
- GetenvUint32
- GetenvUint32Map
- GetenvUint32Slice
- GetenvUint64
- GetenvUint64Map
- GetenvUint64Slice
- GetenvUintMap
- GetenvUintSlice
- HasEnv
- MustGetenv
- MustGetenvBool
- MustGetenvDuration
- MustGetenvFloat32
- MustGetenvFloat64
- MustGetenvInt
- MustGetenvInt8
- MustGetenvInt16
- MustGetenvInt32
- MustGetenvInt64
- MustGetenvUint
- MustGetenvUint8
- MustGetenvUint16
- MustGetenvUint32
- MustGetenvUint64
- MustHasEnv
Constants ¶
This section is empty.
Variables ¶
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 Getenv ¶
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 ¶
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
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
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
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
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
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
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
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
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
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
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
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 ¶
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.