loadenv

package
v1.18.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package loadenv provides easy access to env variables from .env file. Also, loadenv provides option to pass default environments to use, if searched variable will not be found.

Lists (GetEnvAsSlice, IsStringIsValidSlice) follow one contract: a single element is a valid list, elements are trimmed at both ends, empty elements are dropped, and the default value is returned only when nothing is left — the variable is unset, empty, or built of separators alone. An empty separator also gives the default value: splitting by it would yield a slice of characters. The price of trimming is that a value whose edge whitespace is significant will not reach the caller intact; write such whitespace as an escape (`\s`, `[ ]`) rather than a literal.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetEnv

func GetEnv(key, defaultVal string) string

GetEnv is a helper function to read an environment or return a default value.

func GetEnvAsBool

func GetEnvAsBool(name string, defaultVal bool) bool

GetEnvAsBool is a helper to read an environment variable into a bool or return default value.

func GetEnvAsInt

func GetEnvAsInt(name string, defaultVal int) int

GetEnvAsInt is a helper function to read an environment variable into integer or return a default value.

func GetEnvAsSlice

func GetEnvAsSlice(name string, defaultVal []string, separator string) []string

GetEnvAsSlice is a helper function to read an environment variable into a slice or return a default value.

Elements are trimmed and empty ones are dropped, so "a,b" and "a, b" with a "," separator give the same result. The default value is returned only when nothing is left: the variable is unset, empty, or built of separators alone.

A single element is a valid slice. It used to be replaced by the default value without a word, and that is the case every "one origin", "one host", "one allowed method" setting falls into — that is, the one people actually write.

func Init added in v1.12.0

func Init(paths ...string)

Init подразумевает явный вызов и инициализацию переменных с указанным путем перед поулчением переменных окружения.

func IsStringIsValidSlice

func IsStringIsValidSlice(str, separator string) bool

IsStringIsValidSlice is a helper to check, if string is a valid slice to convert it into []string. Can also be used outside loadenv package for other cases.

One element is enough: a list of one is still a list. The former "more than one" rule made every single-valued setting silently fall back to its default.

Types

This section is empty.

Jump to

Keyboard shortcuts

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