env

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 5 Imported by: 0

README

env

import "github.com/brpaz/lib-go/env"

Package env provides helpers for reading typed values from environment variables.

Every Get* function accepts a key and a defaultValue. The default is returned when the variable is unset, empty, or (for numeric/duration helpers) unparseable — invalid values are never fatal.

Every MustGet* function accepts only a key and panics when the variable is unset, empty, or (for numeric/duration helpers) unparseable. Use these for required configuration that has no sensible default (e.g. a database URL or API secret) and should fail fast at startup rather than run with a zero value.

Available helpers
Example
port     := env.GetInt("PORT", 8080)
debug    := env.GetBool("DEBUG", false)
timeout  := env.GetDuration("REQUEST_TIMEOUT", 30*time.Second)
origins  := env.GetStringSlice("ALLOWED_ORIGINS", []string{"http://localhost:3000"})
dbURL    := env.MustGetString("DATABASE_URL")

Package env provides helpers for reading typed values from environment variables.

Index

func GetBool

func GetBool(key string, defaultValue bool) bool

GetBool returns the boolean value of the environment variable named by key. Truthy values: "true", "1". Any other non-empty value returns defaultValue.

func GetDuration

func GetDuration(key string, defaultValue time.Duration) time.Duration

GetDuration returns the time.Duration value of the environment variable named by key, or defaultValue if the variable is not set, empty, or unparseable.

func GetFloat

func GetFloat(key string, defaultValue float64) float64

GetFloat returns the float64 value of the environment variable named by key. Invalid values are silently ignored and defaultValue is returned instead.

func GetInt

func GetInt(key string, defaultValue int) int

GetInt returns the integer value of the environment variable named by key. Invalid values are silently ignored and defaultValue is returned instead.

func GetString

func GetString(key string, defaultValue string) string

GetString returns the value of the environment variable named by key, or defaultValue if the variable is not set or empty.

func GetStringSlice

func GetStringSlice(key string, defaultValue []string) []string

GetStringSlice returns a slice of strings parsed from a comma-separated environment variable. Whitespace around each element is trimmed and empty elements are dropped. Returns defaultValue if the variable is not set or empty.

func MustGetBool

func MustGetBool(key string) bool

MustGetBool returns the boolean value of the environment variable named by key. It panics if the variable is not set, empty, or not one of "true", "1", "false", "0".

func MustGetDuration

func MustGetDuration(key string) time.Duration

MustGetDuration returns the time.Duration value of the environment variable named by key. It panics if the variable is not set, empty, or not a valid duration.

func MustGetFloat

func MustGetFloat(key string) float64

MustGetFloat returns the float64 value of the environment variable named by key. It panics if the variable is not set, empty, or not a valid float.

func MustGetInt

func MustGetInt(key string) int

MustGetInt returns the integer value of the environment variable named by key. It panics if the variable is not set, empty, or not a valid integer.

func MustGetString

func MustGetString(key string) string

MustGetString returns the value of the environment variable named by key. It panics if the variable is not set or empty.

func MustGetStringSlice

func MustGetStringSlice(key string) []string

MustGetStringSlice returns a slice of strings parsed from a comma-separated environment variable named by key. Whitespace around each element is trimmed and empty elements are dropped. It panics if the variable is not set or empty.

Generated by gomarkdoc

Documentation

Overview

Package env provides helpers for reading typed values from environment variables.

Every Get* function accepts a key and a defaultValue. The default is returned when the variable is unset, empty, or (for numeric/duration helpers) unparseable — invalid values are never fatal.

Every MustGet* function accepts only a key and panics when the variable is unset, empty, or (for numeric/duration helpers) unparseable. Use these for required configuration that has no sensible default (e.g. a database URL or API secret) and should fail fast at startup rather than run with a zero value.

Available helpers

Example

port     := env.GetInt("PORT", 8080)
debug    := env.GetBool("DEBUG", false)
timeout  := env.GetDuration("REQUEST_TIMEOUT", 30*time.Second)
origins  := env.GetStringSlice("ALLOWED_ORIGINS", []string{"http://localhost:3000"})
dbURL    := env.MustGetString("DATABASE_URL")

Package env provides helpers for reading typed values from environment variables.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBool

func GetBool(key string, defaultValue bool) bool

GetBool returns the boolean value of the environment variable named by key. Truthy values: "true", "1". Any other non-empty value returns defaultValue.

func GetDuration

func GetDuration(key string, defaultValue time.Duration) time.Duration

GetDuration returns the time.Duration value of the environment variable named by key, or defaultValue if the variable is not set, empty, or unparseable.

func GetFloat

func GetFloat(key string, defaultValue float64) float64

GetFloat returns the float64 value of the environment variable named by key. Invalid values are silently ignored and defaultValue is returned instead.

func GetInt

func GetInt(key string, defaultValue int) int

GetInt returns the integer value of the environment variable named by key. Invalid values are silently ignored and defaultValue is returned instead.

func GetString

func GetString(key string, defaultValue string) string

GetString returns the value of the environment variable named by key, or defaultValue if the variable is not set or empty.

func GetStringSlice

func GetStringSlice(key string, defaultValue []string) []string

GetStringSlice returns a slice of strings parsed from a comma-separated environment variable. Whitespace around each element is trimmed and empty elements are dropped. Returns defaultValue if the variable is not set or empty.

func MustGetBool

func MustGetBool(key string) bool

MustGetBool returns the boolean value of the environment variable named by key. It panics if the variable is not set, empty, or not one of "true", "1", "false", "0".

func MustGetDuration

func MustGetDuration(key string) time.Duration

MustGetDuration returns the time.Duration value of the environment variable named by key. It panics if the variable is not set, empty, or not a valid duration.

func MustGetFloat

func MustGetFloat(key string) float64

MustGetFloat returns the float64 value of the environment variable named by key. It panics if the variable is not set, empty, or not a valid float.

func MustGetInt

func MustGetInt(key string) int

MustGetInt returns the integer value of the environment variable named by key. It panics if the variable is not set, empty, or not a valid integer.

func MustGetString

func MustGetString(key string) string

MustGetString returns the value of the environment variable named by key. It panics if the variable is not set or empty.

func MustGetStringSlice

func MustGetStringSlice(key string) []string

MustGetStringSlice returns a slice of strings parsed from a comma-separated environment variable named by key. Whitespace around each element is trimmed and empty elements are dropped. It panics if the variable is not set or empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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