Documentation
¶
Index ¶
- func Getenv(key string, def string) string
- func GetenvBool(key string, def bool) (bool, error)
- func GetenvFloat32(key string, def float32) (float32, error)
- func GetenvFloat64(key string, def float64) (float64, error)
- func GetenvInt32(key string, def int32) (int32, error)
- func GetenvInt64(key string, def int64) (int64, error)
- func GetenvStringMapString(key string, def map[string]string) (map[string]string, error)
- func GetenvStringSlice(key string, def []string) []string
- func HasEnv(key string) bool
- func MustHasEnv(key string)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
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 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 GetenvFloat64 ¶ added in v0.0.2
GetenvFloat64 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.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 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 GetenvInt64 ¶ added in v0.0.2
GetenvInt64 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.GetenvInt64("FOO", 1)
fmt.Println(v)
_ = os.Setenv("FOO", "2")
v, _ = osx.GetenvInt64("FOO", 1)
fmt.Println(v)
}
Output: 1 2
func GetenvStringMapString ¶ added in v0.2.0
GetenvStringMapString 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.GetenvStringMapString("FOO", nil)
fmt.Println(v)
_ = os.Setenv("FOO", " x : hello , y : world ")
v, _ = osx.GetenvStringMapString("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.GetenvStringMapString("FOO", nil)
fmt.Println(err != nil)
}
Output: map[a:1] x:hello y:world true
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 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 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.