env

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package env provides utilities for reading environment variables with type conversion and fallback values.

All functions return a fallback value if the environment variable is not set or cannot be parsed to the requested type.

Usage

port := env.GetAsInt("PORT", 8080)
debug := env.GetAsBool("DEBUG", false)
hosts := env.GetAsStringArr("HOSTS", "localhost")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(key, fallback string) string

Get returns the value of the environment variable with the given key, or the fallback value if the key is not set

Parameters:

  • key: The key of the environment variable
  • fallback: The fallback value if the key is not set

Returns:

  • string: The value of the environment variable

func GetAsBool

func GetAsBool(key string, fallback bool) bool

GetAsBool returns the value of the environment variable with the given key, or the fallback value if the key is not set

Parameters:

  • key: The key of the environment variable
  • fallback: The fallback value if the key is not set

Returns:

  • bool: The value of the environment variable

func GetAsBoolArr

func GetAsBoolArr(key string, fallback string) []bool

GetAsBoolArr returns the value of the environment variable as a boolean slice. Values are expected to be comma-separated booleans. Invalid booleans are skipped.

Example:

// FEATURES=true,false,true
features := env.GetAsBoolArr("FEATURES", "false")

func GetAsFloat64

func GetAsFloat64(key string, fallback float64) float64

GetAsFloat64 returns the value of the environment variable as a float64. Returns the fallback value if the variable is not set or cannot be parsed.

Example:

// TIMEOUT=30.5
timeout := env.GetAsFloat64("TIMEOUT", 10.0)

func GetAsFloat64Arr

func GetAsFloat64Arr(key string, fallback string) []float64

GetAsFloat64Arr returns the value of the environment variable as a float64 slice. Values are expected to be comma-separated floats. Invalid floats are skipped.

Example:

// WEIGHTS=0.5,0.3,0.2
weights := env.GetAsFloat64Arr("WEIGHTS", "1.0")

func GetAsInt

func GetAsInt(key string, fallback int) int

GetAsInt returns the value of the environment variable with the given key, or the fallback value if the key is not set

Parameters:

  • key: The key of the environment variable
  • fallback: The fallback value if the key is not set

Returns:

  • int: The value of the environment variable

func GetAsIntArr

func GetAsIntArr(key string, fallback string) []int

GetAsIntArr returns the value of the environment variable as an integer slice. Values are expected to be comma-separated integers. Invalid integers are skipped.

Example:

// PORTS=8080,8081,8082
ports := env.GetAsIntArr("PORTS", "8080")

func GetAsStringArr

func GetAsStringArr(key, fallback string) []string

GetAsStringArr returns the value of the environment variable as a string slice. Values are expected to be comma-separated. Returns the fallback split by commas if the environment variable is not set.

Example:

// HOSTS=host1,host2,host3
hosts := env.GetAsStringArr("HOSTS", "localhost")

Types

This section is empty.

Jump to

Keyboard shortcuts

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