Categorygithub.com/dmitrymomot/go-env
modulepackage
1.0.2
Repository: https://github.com/dmitrymomot/go-env.git
Documentation: pkg.go.dev

# 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)
}

# Functions

GetBool func returns environment variable value as a boolean value, If variable doesn't exist or is not set, returns fallback value.
GetBytes func returns environment variable value as a bytes slice If variable doesn't exist or is not set, returns fallback value.
GetDuration func returns environment variable value as a parsed duration value, If variable doesn't exist, is not set or unparsable, returns fallback value.
GetFloat func returns environment variable value as a float value, If variable doesn't exist or is not set, returns fallback value.
GetFloats func returns environment variable value as a float slice If variable doesn't exist or is not set, returns fallback value.
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 "=".
GetInt func returns environment variable value as a integer value, If variable doesn't exist or is not set, returns fallback value.
GetInts func returns environment variable value as a integer slice If variable doesn't exist or is not set, returns fallback value.
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 "=".
GetString func returns environment variable value as a string value, If variable doesn't exist or is not set, returns fallback value.
GetStrings func returns environment variable value as a string slice If variable doesn't exist or is not set, returns fallback value.
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 "=".
GetTime func returns environment variable value as a parsed time value, If variable doesn't exist, is not set or unparsable, returns fallback value.
MustBool func returns environment variable value as a boolean value, If variable doesn't exist or is not set, exits from the runtime.
MustBytes func returns environment variable value as a bytes slice.
MustDuration func returns environment variable value as a parsed duration value, If variable doesn't exist, is not set or unparsable, then panics.
MustFloat func returns environment variable value as a float value, If variable doesn't exist or is not set, exits from the runtime.
MustFloats func returns environment variable value as a float slice.
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.
MustInt func returns environment variable value as an integer value, If variable doesn't exist or is not set, exits from the runtime.
MustInts func returns environment variable value as an integer slice.
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.
MustString func returns environment variable value as a string value, If variable doesn't exist or is not set, exits from the runtime.
MustStrings func returns environment variable value as a string slice.
MustStringsMap func returns environment variable value as a string map.
MustTime func returns environment variable value as a parsed time value, If variable doesn't exist, is not set or unparsable, then panics.