utils

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// This is for backward compatibility, once all app using this are upgraded
	// remove below aliases.  These functions are depcrecated and renamed
	InitEnv         func(...string)             = LoadEnvEach
	LoadEnv         func(...string)             = LoadEnvAll
	LoadEnvironment func(...string)             = ReloadEnvEach
	GetEnv          func(string, string) string = GetEnvString
)

Functions

func DurationBiggerThanZero

func DurationBiggerThanZero(a, b time.Duration) bool

func GetEnvBool

func GetEnvBool(key string, defaultValue bool) bool

GetBool retrieves a boolean environment variable or returns a default value

func GetEnvDuration

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

GetDuration retrieves a duration environment variable or returns a default value

func GetEnvInt

func GetEnvInt(key string, defaultValue int) int

GetEnv for integer with option for default value if not set

func GetEnvString

func GetEnvString(key string, defaultValue string) string

GetEnv retrieves an environment variable or returns a default value if not set.

func IntABiggerThanB

func IntABiggerThanB(a, b int) bool

func IntASmallerThanB

func IntASmallerThanB(a, b int) bool

func IntBiggerThanZero

func IntBiggerThanZero(a, b int) bool

If a is bigger than 0

func LoadEnvAll

func LoadEnvAll(envFiles ...string)

LoadEnvAll loads environment variables from .env file and optional additional files. This will load all the files in the order they are provided, skipping any existing variables with the same name. If a file does not exist, it will be ignored without error. This is the default behavior of godotenv.Load().

func LoadEnvEach

func LoadEnvEach(envFiles ...string)

Read one by one and error accordingly

func ReloadEnvEach

func ReloadEnvEach(additionalFiles ...string)

LoadEnvironment loads environment variables from .env file and optional additional files. This use the Overload() means it will overwrite the variables if already exist, Load() won't

func ValueOrDefault

func ValueOrDefault[T comparable](value, preset T, compF ComparisonFunction[T]) T

Used to get value usually from environment if value is true via ComparisonFunction use value, if not use preset (or default value) Ex: ValueOrDefault(time.Duration(timeout)*time.Second, DEFAULT_TIMEOUT, DurationBiggerThanZero),

Set with maxIdle if bigger than 0 or use DEFAULT_MAX_IDLE MaxIdleConns = ValueOrDefault(maxIdle, DEFAULT_MAX_IDLE_CONNECTIONS, IntBiggerThanZero),

Set with maxIdle if it's bigger than DEFAULT_MAX_IDLE or use DEFAULT_MAX_IDLE MaxIdleConns = ValueOrDefault(maxIdle, DEFAULT_MAX_IDLE, IntABiggerThanB),

Types

type ComparisonFunction

type ComparisonFunction[T comparable] func(a, b T) bool

This is used for comparing to something, some use only 1 variable (a) sometimes we need 2 variables. Ex: func(a,b) { return a> 0} -- this means if a > 0 then return true . func(a,b) { return a > b} -- this meansi f a > b then return true This is used for comparison ValueOrDefault() function Which if the ComparisonFunction returns true, return a, if not return b

Jump to

Keyboard shortcuts

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