env

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2023 License: Apache-2.0 Imports: 5 Imported by: 26

README

go-env

Go Reference Go Report Card Tests

Helpers to work with environment variables.

Usage Example

package main

import (
    "fmt"
    "net/http"
    "github.com/dmitrymomot/go-env"
    "github.com/go-chi/chi"
)

var (
    httpPort = env.MustInt("HTTP_PORT") // required env variable, throws fatal error if it empty
    healthEndpoint = env.GetString("HEALTH_ENDPOINT", "/health") // optional env variable with fallback value
)

func main() {
    r := chi.NewRouter()
    r.Get(healthEndpoint, func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusNoContent)
    })
    http.ListenAndServe(fmt.Sprintf(":%d", httpPort), r)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBool

func GetBool(key string, fallback bool) bool

GetBool func returns environment variable value as a boolean value, If variable doesn't exist or is not set, returns fallback value

func GetBytes added in v0.1.1

func GetBytes(key string, fallback []byte) []byte

GetBytes func returns environment variable value as a bytes slice If variable doesn't exist or is not set, returns fallback value

func GetDuration

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

GetDuration func returns environment variable value as a parsed duration value, If variable doesn't exist, is not set or unparsable, returns fallback value

func GetFloat

func GetFloat[T float32 | float64](key string, fallback T) T

GetFloat func returns environment variable value as a float value, If variable doesn't exist or is not set, returns fallback value

func GetFloats added in v1.0.2

func GetFloats[T float32 | float64](key string, sep string, fallback []T) []T

GetFloats func returns environment variable value as a float slice If variable doesn't exist or is not set, returns fallback value

func GetFloatsMap added in v1.0.2

func GetFloatsMap[T float32 | float64](key string, sep string, kvSep string, fallback map[string]T) map[string]T

GetFloatsMap func returns environment variable value as a map[string]float[32|64] If variable doesn't exist or is not set, returns fallback value Example: key=1.2,key2=0.3 sep - key value separator, default is "," kvSep - key value separator, default is "="

func GetInt

func GetInt[T int | int16 | int32 | int64](key string, fallback T) T

GetInt func returns environment variable value as a integer value, If variable doesn't exist or is not set, returns fallback value

func GetInts added in v1.0.2

func GetInts[T int | int16 | int32 | int64](key string, sep string, fallback []T) []T

GetInts func returns environment variable value as a integer slice If variable doesn't exist or is not set, returns fallback value

func GetIntsMap added in v1.0.2

func GetIntsMap[T int | int16 | int32 | int64](key string, sep string, kvSep string, fallback map[string]T) map[string]T

GetIntsMap func returns environment variable value as a map[string]int[16|32|64] If variable doesn't exist or is not set, returns fallback value Example: key=2,key2=32 sep - key value separator, default is "," kvSep - key value separator, default is "="

func GetString

func GetString(key string, fallback string) string

GetString func returns environment variable value as a string value, If variable doesn't exist or is not set, returns fallback value

func GetStrings added in v1.0.2

func GetStrings(key string, sep string, fallback []string) []string

GetStrings func returns environment variable value as a string slice If variable doesn't exist or is not set, returns fallback value

func GetStringsMap added in v1.0.2

func GetStringsMap(key string, sep string, kvSep string, fallback map[string]string) map[string]string

GetStringsMap func returns environment variable value as a map[string]string If variable doesn't exist or is not set, returns fallback value Example: key=value1,key2=value2 sep - key value separator, default is "," kvSep - key value separator, default is "="

func GetTime added in v1.0.0

func GetTime(key, format string, fallback time.Time) time.Time

GetTime func returns environment variable value as a parsed time value, If variable doesn't exist, is not set or unparsable, returns fallback value. If format is empty, then time.RFC3339 is used.

func MustBool

func MustBool(key string) bool

MustBool func returns environment variable value as a boolean value, If variable doesn't exist or is not set, exits from the runtime

func MustBytes added in v0.1.1

func MustBytes(key string) []byte

MustBytes func returns environment variable value as a bytes slice. If variable doesn't exist or is not set, exits from the runtime.

func MustDuration

func MustDuration(key string) time.Duration

MustDuration func returns environment variable value as a parsed duration value, If variable doesn't exist, is not set or unparsable, then panics

func MustFloat

func MustFloat[T float32 | float64](key string) T

MustFloat func returns environment variable value as a float value, If variable doesn't exist or is not set, exits from the runtime

func MustFloats added in v1.0.2

func MustFloats[T float32 | float64](key string, sep string) []T

MustFloats func returns environment variable value as a float slice. If variable doesn't exist or is not set, exits from the runtime.

func MustFloatsMap added in v1.0.2

func MustFloatsMap[T float32 | float64](key string, sep string, kvSep string) map[string]T

MustFloatsMap func returns environment variable value as a map[string]float[32|64] If variable doesn't exist or is not set, exits from the runtime. Example: key=1.2,key2=0.3 sep - key value separator, default is "," kvSep - key value separator, default is "="

func MustInt

func MustInt[T int | int16 | int32 | int64](key string) T

MustInt func returns environment variable value as an integer value, If variable doesn't exist or is not set, exits from the runtime

func MustInts added in v1.0.2

func MustInts[T int | int16 | int32 | int64](key string, sep string) []T

MustInts func returns environment variable value as an integer slice. If variable doesn't exist or is not set, exits from the runtime.

func MustIntsMap added in v1.0.2

func MustIntsMap[T int | int16 | int32 | int64](key string, sep string, kvSep string) map[string]T

MustIntsMap func returns environment variable value as a map[string]int[16|32|64] If variable doesn't exist or is not set, exits from the runtime. Example: key=2,key2=32 sep - key value separator, default is "," kvSep - key value separator, default is "="

func MustString

func MustString(key string) string

MustString func returns environment variable value as a string value, If variable doesn't exist or is not set, exits from the runtime

func MustStrings added in v1.0.2

func MustStrings(key string, sep string) []string

MustStrings func returns environment variable value as a string slice. If variable doesn't exist or is not set, exits from the runtime.

func MustStringsMap added in v1.0.2

func MustStringsMap(key string, sep string, kvSep string) map[string]string

MustStringsMap func returns environment variable value as a string map. If variable doesn't exist or is not set, exits from the runtime.

func MustTime added in v1.0.0

func MustTime(key string, format string) time.Time

MustTime func returns environment variable value as a parsed time value, If variable doesn't exist, is not set or unparsable, then panics. If format is empty, then time.RFC3339 is used. See default time formats: https://golang.ir/pkg/time/#pkg-constants

Types

This section is empty.

Jump to

Keyboard shortcuts

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