os

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

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 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 GetenvFloat64 added in v0.0.2

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

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

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 GetenvInt64 added in v0.0.2

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

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

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

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

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 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 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