getenv

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2023 License: MIT Imports: 3 Imported by: 2

README

coverbadger-tag-do-not-edit

GitHub go.mod Go version Go Reference Go Report Card codecov Quality Gate Status

getenv

Package getenv provides functionality for loading environment variables and parse them into go builtin types.

Types supported:

  • string
  • []string
  • int
  • []int
  • int64
  • []int64
  • float64
  • []float64
  • time.Time
  • time.Duration
  • bool

Documentation

Overview

Package getenv provides functionality for loading environment variables.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolOrDefault deprecated

func BoolOrDefault(key string, defaultVal bool) bool

BoolOrDefault retrieves the bool value of the environment variable named by the key. If variable not set or value is empty - defaultVal will be returned.

Deprecated: use EnvOrDefault.

func DurationOrDefault deprecated

func DurationOrDefault(key string, defaultVal time.Duration) time.Duration

DurationOrDefault retrieves the time.Duration value of the environment variable named by the key. If variable not set or value is empty - defaultVal will be returned.

Deprecated: use EnvOrDefault.

func EnvOrDefault added in v1.2.0

func EnvOrDefault[T internal.EnvParsable](key string, defaultVal T, options ...option.Option) T

EnvOrDefault retrieves the value of the environment variable named by the key. If variable not set or value is empty - defaultVal will be returned.

Example
key := "GH_GETENV_TEST"

defer func() {
	if err := os.Unsetenv("GH_GETENV_TEST"); err != nil {
		panic(err)
	}
}()

var val any

// string
if err := os.Setenv(key, "golly"); err != nil {
	panic(err)
}

val = EnvOrDefault(key, "golly")
fmt.Printf("[%T]: %v\n", val, val)

// int
if err := os.Setenv(key, "123"); err != nil {
	panic(err)
}

val = EnvOrDefault(key, -99)
fmt.Printf("[%T]: %v\n", val, val)

// time.Time
if err := os.Setenv(key, "2022-01-20"); err != nil {
	panic(err)
}

val = EnvOrDefault(key,
	time.Date(1992, 12, 1, 0, 0, 0, 0, time.UTC),
	option.WithTimeLayout("2006-01-02"),
)
fmt.Printf("[%T]: %v\n", val, val)

// []float64
if err := os.Setenv(key, "26.89,0.67"); err != nil {
	panic(err)
}

val = EnvOrDefault(key, []float64{-99},
	option.WithSeparator(","),
)
fmt.Printf("[%T]: %v\n", val, val)

// time.Duration
if err := os.Setenv(key, "2h35m"); err != nil {
	panic(err)
}

val = EnvOrDefault(key, time.Second)
fmt.Printf("[%T]: %v\n", val, val)
Output:

[string]: golly
[int]: 123
[time.Time]: 2022-01-20 00:00:00 +0000 UTC
[[]float64]: [26.89 0.67]
[time.Duration]: 2h35m0s

func Float64OrDefault deprecated

func Float64OrDefault(key string, defaultVal float64) float64

Float64OrDefault retrieves the float64 value of the environment variable named by the key. If variable not set or value is empty - defaultVal will be returned.

Deprecated: use EnvOrDefault.

func Float64SliceOrDefault deprecated added in v1.2.0

func Float64SliceOrDefault(key string, defaultVal []float64, sep string) []float64

Float64SliceOrDefault retrieves the float64 slice value of the environment variable named by the key and separated by sep. If variable not set or value is empty - defaultVal will be returned.

Deprecated: use EnvOrDefault.

func Int64OrDefault deprecated

func Int64OrDefault(key string, defaultVal int64) int64

Int64OrDefault retrieves the int64 value of the environment variable named by the key. If variable not set or value is empty - defaultVal will be returned.

Deprecated: use EnvOrDefault.

func Int64SliceOrDefault deprecated added in v1.2.0

func Int64SliceOrDefault(key string, defaultVal []int64, sep string) []int64

Int64SliceOrDefault retrieves the int6464 slice value of the environment variable named by the key and separated by sep. If variable not set or value is empty - defaultVal will be returned.

Deprecated: use EnvOrDefault.

func IntOrDefault deprecated

func IntOrDefault(key string, defaultVal int) int

IntOrDefault retrieves the int value of the environment variable named by the key. If variable not set or value is empty - defaultVal will be returned.

Deprecated: use EnvOrDefault.

func IntSliceOrDefault deprecated added in v1.2.0

func IntSliceOrDefault(key string, defaultVal []int, sep string) []int

IntSliceOrDefault retrieves the int slice value of the environment variable named by the key and separated by sep. If variable not set or value is empty - defaultVal will be returned.

Deprecated: use EnvOrDefault.

func StringOrDefault deprecated

func StringOrDefault(key, defaultVal string) string

StringOrDefault retrieves the string value of the environment variable named by the key. If variable not set or value is empty - defaultVal will be returned.

Deprecated: use EnvOrDefault.

func StringSliceOrDefault deprecated

func StringSliceOrDefault(key string, defaultVal []string, sep string) []string

StringSliceOrDefault retrieves the string slice value of the environment variable named by the key and separated by sep. If variable not set or value is empty - defaultVal will be returned.

Deprecated: use EnvOrDefault.

func TimeOrDefault deprecated

func TimeOrDefault(key string, defaultVal time.Time, layout string) time.Time

TimeOrDefault retrieves the time.Time value of the environment variable named by the key represented by layout. If variable not set or value is empty - defaultVal will be returned.

Deprecated: use EnvOrDefault.

Types

This section is empty.

Directories

Path Synopsis
Package internal provides internal implementation logic for environment variables parsing.
Package internal provides internal implementation logic for environment variables parsing.
Package option provides options for parsing environment variables.
Package option provides options for parsing environment variables.

Jump to

Keyboard shortcuts

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