envconv

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2025 License: MIT Imports: 5 Imported by: 0

README

envconv

CI Test Go Reference

Utility functions to parse environment variables

One scenario where these functions could be useful is when they are used in combination with the flag package

var (
    logLevel slog.Level
    timeout time.Duration
    ...
)

func main() {
    flag.TextVar(&logLevel, "log.level", envconv.GetTextUnmarshaler("APP_LOG_LEVEL", slog.LevelInfo), "application log level")
    flag.TextVar(&timeout, "timeout", envconv.GetDuration("APP_TIMEOUT", 30*time.Second), "application timeout")
    ...
}

Documentation

Overview

package envconv provides utilities for converting environment variable values

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBool

func GetBool(name string, defaultValue bool) bool

GetBool retrieves the boolean value of the environment variable named by the key.

The conversion follows strconv.ParseBool rules. If the variable is not set or cannot be converted to a boolean, it returns the provided defaultValue.

func GetDuration

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

GetDuration retrieves the time.Duration value of the environment variable named by the key.

The conversion follows time.ParseDuration rules. If the variable is not set or cannot be converted to a time.Duration, it returns the provided defaultValue.

func GetInt

func GetInt(name string, defaultValue int) int

GetInt retrieves the integer value of the environment variable named by the key.

The conversion follows strconv.Atoi rules. If the variable is not set or cannot be converted to an integer, it returns the provided defaultValue.

func GetSlogLevel deprecated

func GetSlogLevel(name string, defaultValue slog.Level) slog.Level

GetSlogLevel retrieves the slog.Level value of the environment variable named by the key.

The conversion follows slog.Level.UnmarshalText rules. If the variable is not set or cannot be converted to a slog.Level, it returns the provided defaultValue.

Deprecated: use GetTextUnmarshaler instead.

func GetString

func GetString(name, defaultValue string) string

GetString retrieves the string value of the environment variable named by the key.

If the variable is not set, it returns the provided defaultValue.

func GetTextUnmarshaler added in v0.2.0

func GetTextUnmarshaler[T any, TPtr interface {
	*T
	encoding.TextUnmarshaler
}](name string, defaultValue T) T

GetTextUnmarshaler retrieves a value of type T that implements encoding.TextUnmarshaler from the environment variable named by the key.

If the variable is not set or cannot be converted, it returns the provided defaultValue. The type TPtr is a pointer to T that implements encoding.TextUnmarshaler.

Example usage:

var slogLevel slog.Level = envconv.GetTextUnmarshaler("MY_LOG_LEVEL", slog.LevelInfo)
var netIP net.IP = envconv.GetTextUnmarshaler("MY_IP", net.IPv4(192.168, 0, 1))

Types

This section is empty.

Jump to

Keyboard shortcuts

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