env

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2023 License: MIT Imports: 4 Imported by: 0

README

env

GoDoc License

env is a simple Go package that makes it easy to retrieve environment variables and provide fallback values if the specified key is not present. It also offers parsing of environment variables to various data types.

This package is load or parse the dot env file. To load the dot env file, use godotenv package.

Installation

go get github.com/pkg-id/env

Usage

package main

import (
	"fmt"
	"github.com/pkg-id/env"
	"time"
)

func main() {
	// Get the value of the "FOO" environment variable or return "defaultFoo" if it doesn't exist.
	foo := env.String("FOO", "defaultFoo")
	fmt.Println(foo)

	// Get the value of the "BAR" environment variable as an integer or return 42 if it doesn't exist.
	bar := env.Int("BAR", 42)
	fmt.Println(bar)

	// Get the value of the "BAZ" environment variable as a float64 or return 3.14 if it doesn't exist.
	baz := env.Float64("BAZ", 3.14)
	fmt.Println(baz)

	// Get the value of the "QUX" environment variable as a boolean or return false if it doesn't exist.
	qux := env.Bool("QUX", false)
	fmt.Println(qux)

	// Get the value of the "MY_DURATION" environment variable as a time.Duration or return 10 seconds if it doesn't exist.
	duration := env.Duration("MY_DURATION", 10*time.Second)
	fmt.Println(duration)

	// Get the value of the "MY_LIST" environment variable as a list of integers or return []int{1, 2, 3} if it doesn't exist.
	list := env.List("MY_LIST", env.Parsers.Int(), []int{1, 2, 3})
	fmt.Println(list)
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Overview

Package env provides a straightforward way to retrieve environment variables and offers a default value if the specified key is not present.

Index

Constants

View Source
const Parsers = parsers(1)

Parsers is a namespace that can be used to access all available parsers.

Variables

This section is empty.

Functions

func Bool

func Bool(key string, fallback bool) bool

Bool returns the bool value if the key exists; otherwise, it returns the fallback value.

func Duration

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

Duration returns the time.Duration value if the key exists; otherwise, it returns the fallback value.

func Float64

func Float64(key string, fallback float64) float64

Float64 returns the float64 value if the key exists; otherwise, it returns the fallback value.

func Int

func Int(key string, fallback int) int

Int returns the int value if the key exists; otherwise, it returns the fallback value.

func Int64

func Int64(key string, fallback int64) int64

Int64 returns the int64 value if the key exists; otherwise, it returns the fallback value.

func List

func List[T any](key string, parser Parser[T], fallback []T) []T

List retrieves the environment variable with the specified key and attempts to parse it into a slice of type T. The Parser function is used to parse each value in the comma-separated string obtained from the environment variable. If the environment variable is not set or parsing fails for any of the values, the function returns the fallback value.

func Parse

func Parse[T any](key string, parser Parser[T], fallback T) T

Parse the env value to the given type. Parse is a generic function that takes the key, parser, and a fallback value as arguments. It returns the parsed value of the environment variable if the key exists, or the fallback value if it does not.

func String

func String(key, fallback string) string

String returns the string value if the key exists; otherwise, it returns the fallback value.

Types

type Parser

type Parser[T any] func(v string) (T, error)

Parser is a generic function type that takes a string input and returns a value of type T along with an error.

Jump to

Keyboard shortcuts

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