os
Typed environment variable parsing with defaults.
Import
import osx "github.com/foomo/go/os"API
HasEnv
func HasEnv(key string) boolReturns true if the environment variable is defined.
MustHasEnv
func MustHasEnv(key string)Panics if the environment variable is not defined.
Getenv
func Getenv(key string, def string) stringReturns the environment variable value, or the default if empty or undefined.
GetenvBool
func GetenvBool(key string, def bool) (bool, error)Parses the environment variable as a boolean, or returns the default.
GetenvInt32
func GetenvInt32(key string, def int32) (int32, error)Parses the environment variable as an int32 (supports 0x hex prefix), or returns the default.
GetenvInt64
func GetenvInt64(key string, def int64) (int64, error)Parses the environment variable as an int64, or returns the default.
GetenvFloat32
func GetenvFloat32(key string, def float32) (float32, error)Parses the environment variable as a float32, or returns the default.
GetenvFloat64
func GetenvFloat64(key string, def float64) (float64, error)Parses the environment variable as a float64, or returns the default.
GetenvStringSlice
func GetenvStringSlice(key string, def []string) []stringParses a comma-separated environment variable into a string slice. Values are space-trimmed.
GetenvStringMapString
func GetenvStringMapString(key string, def map[string]string) (map[string]string, error)Parses a comma-separated list of key:value pairs into a map. Keys and values are space-trimmed.
Examples
Basic usage
package main
import (
"fmt"
"os"
osx "github.com/foomo/go/os"
)
func main() {
// String with default
host := osx.Getenv("APP_HOST", "localhost")
fmt.Println(host)
// Check existence
if osx.HasEnv("DATABASE_URL") {
fmt.Println("database configured")
}
// Typed parsing
os.Setenv("DEBUG", "true")
debug, _ := osx.GetenvBool("DEBUG", false)
fmt.Println(debug) // true
os.Setenv("PORT", "8080")
port, _ := osx.GetenvInt32("PORT", 3000)
fmt.Println(port) // 8080
}Slices and maps
os.Setenv("ALLOWED_ORIGINS", "foo.com, bar.com, baz.com")
origins := osx.GetenvStringSlice("ALLOWED_ORIGINS", nil)
// origins = ["foo.com", "bar.com", "baz.com"]
os.Setenv("LABELS", "env:prod, region:eu")
labels, _ := osx.GetenvStringMapString("LABELS", nil)
// labels = map["env":"prod", "region":"eu"]