env

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: MIT Imports: 4 Imported by: 2

README

Env

Golang Get Environment Variables Package

Build Status Go Report Card Codecov Go Reference License

Install

go get github.com/nasermirzaei89/env

Sample Usage

With default value
package main

import (
	"fmt"

	"github.com/nasermirzaei89/env"
)

func main() {
	b := env.GetBool("A", true)
	fmt.Println(b) // true (default)

	f := env.GetFloat64("B", 14.5)
	fmt.Println(f) // 14.5 (default)

	i := env.GetInt("C", 12)
	fmt.Println(i) // 12 (default)

	s := env.GetString("B", "hi")
	fmt.Println(s) // hi (default)

	// Generics

	b2 := env.Get("A", true)
	fmt.Println(b2) // true (default)

	f2 := env.Get("B", 14.5)
	fmt.Println(f2) // 14.5 (default)

	i2 := env.Get("C", 12)
	fmt.Println(i2) // 12 (default)

	s2 := env.Get("B", "hi")
	fmt.Println(s2) // hi (default)
}
Force setting environment
package main

import (
	"fmt"

	"github.com/nasermirzaei89/env"
)

func main() {
	s := env.MustGetString("HOME")
	fmt.Println(s) // /Users/nasermirzaei89

	s = env.MustGetString("NEW") // panics

	// Generics

	s2 := env.MustGet[string]("HOME")
	fmt.Println(s2) // /Users/nasermirzaei89

	s2 = env.MustGet[string]("NEW") // panics
}

Contributing

You can submit a new issue in GitHub issues. Or you can create a fork, hack on your fork and when you're done create a pull request, so that the code contribution can get merged into the main package.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get added in v1.6.0

func Get[T Type](key string, def T) T

Get extracts value from env based on its type. if not set, returns default value.

func GetBool

func GetBool(key string, def bool) bool

GetBool extracts bool value from env. if not set, returns default value.

func GetFloat32 added in v1.1.0

func GetFloat32(key string, def float32) float32

GetFloat32 extracts int value from env, if not set, returns default value.

func GetFloat32Slice added in v1.5.0

func GetFloat32Slice(key string, def []float32) []float32

GetFloat32Slice extracts slice of float32 value with the format "1.2,2.3,3.4" from env. if not set, returns default value.

func GetFloat64 added in v1.1.0

func GetFloat64(key string, def float64) float64

GetFloat64 extracts int value from env. if not set, returns default value.

func GetFloat64Slice added in v1.5.0

func GetFloat64Slice(key string, def []float64) []float64

GetFloat64Slice extracts slice of float64 value with the format "1.2,2.3,3.4" from env. if not set, returns default value.

func GetInt

func GetInt(key string, def int) int

GetInt extracts int value from env. if not set, returns default value.

func GetInt16 added in v1.1.0

func GetInt16(key string, def int16) int16

GetInt16 extracts int16 value from env. if not set, returns default value.

func GetInt16Slice added in v1.4.0

func GetInt16Slice(key string, def []int16) []int16

GetInt16Slice extracts slice of int16 value with the format "1,2,3" from env. if not set, returns default value.

func GetInt32 added in v1.1.0

func GetInt32(key string, def int32) int32

GetInt32 extracts int32 value from env. if not set, returns default value.

func GetInt32Slice added in v1.4.0

func GetInt32Slice(key string, def []int32) []int32

GetInt32Slice extracts slice of int32 value with the format "1,2,3" from env. if not set, returns default value.

func GetInt64 added in v1.1.0

func GetInt64(key string, def int64) int64

GetInt64 extracts int64 value from env. if not set, returns default value.

func GetInt64Slice added in v1.4.0

func GetInt64Slice(key string, def []int64) []int64

GetInt64Slice extracts slice of int64 value with the format "1,2,3" from env. if not set, returns default value.

func GetInt8 added in v1.1.0

func GetInt8(key string, def int8) int8

GetInt8 extracts int8 value from env. if not set, returns default value.

func GetInt8Slice added in v1.4.0

func GetInt8Slice(key string, def []int8) []int8

GetInt8Slice extracts slice of int8 value with the format "1,2,3" from env. if not set, returns default value.

func GetIntSlice added in v1.4.0

func GetIntSlice(key string, def []int) []int

GetIntSlice extracts slice of int value with the format "1,2,3" from env. if not set, returns default value.

func GetString

func GetString(key, def string) string

GetString extracts string value from env. if not set, returns default value.

func GetStringSlice added in v1.2.0

func GetStringSlice(key string, def []string) []string

GetStringSlice extracts slice of strings value with format "foo,bar,baz" from env. if not set, returns default value.

func GetUint added in v1.4.0

func GetUint(key string, def uint) uint

GetUint extracts uint value from env. if not set, returns default value.

func GetUint16 added in v1.4.0

func GetUint16(key string, def uint16) uint16

GetUint16 extracts uint16 value from env. if not set, returns default value.

func GetUint16Slice added in v1.4.0

func GetUint16Slice(key string, def []uint16) []uint16

GetUint16Slice extracts slice of uint16 value with the format "1,2,3" from env. if not set, returns default value.

func GetUint32 added in v1.4.0

func GetUint32(key string, def uint32) uint32

GetUint32 extracts uint32 value from env. if not set, returns default value.

func GetUint32Slice added in v1.4.0

func GetUint32Slice(key string, def []uint32) []uint32

GetUint32Slice extracts slice of uint32 value with the format "1,2,3" from env. if not set, returns default value.

func GetUint64 added in v1.4.0

func GetUint64(key string, def uint64) uint64

GetUint64 extracts uint64 value from env. if not set, returns default value.

func GetUint64Slice added in v1.4.0

func GetUint64Slice(key string, def []uint64) []uint64

GetUint64Slice extracts slice of uint64 value with the format "1,2,3" from env. if not set, returns default value.

func GetUint8 added in v1.4.0

func GetUint8(key string, def uint8) uint8

GetUint8 extracts uint8 value from env. if not set, returns default value.

func GetUint8Slice added in v1.4.0

func GetUint8Slice(key string, def []uint8) []uint8

GetUint8Slice extracts slice of uint8 value with the format "1,2,3" from env. if not set, returns default value.

func GetUintSlice added in v1.4.0

func GetUintSlice(key string, def []uint) []uint

GetUintSlice extracts slice of uint value with the format "1,2,3" from env. if not set, returns default value.

func Is

func Is(e1 Env, ee ...Env) bool

Is checks whether ENV is what you expected or not.

func IsDevelopment

func IsDevelopment() bool

IsDevelopment checks whether ENV is development or not.

func IsProduction

func IsProduction() bool

IsProduction checks whether ENV is production or not.

func IsStaging

func IsStaging() bool

IsStaging checks whether ENV is staging or not.

func IsTesting

func IsTesting() bool

IsTesting checks whether ENV is testing or not.

func MustGet added in v1.6.0

func MustGet[T Type](key string) T

MustGet extracts string value from env based on its type. if not set, it panics.

func MustGetBool

func MustGetBool(key string) bool

MustGetBool extracts bool value from env. if not set, it panics.

func MustGetFloat32 added in v1.1.0

func MustGetFloat32(key string) float32

MustGetFloat32 extracts int value from env. if not set, it panics.

func MustGetFloat32Slice added in v1.5.0

func MustGetFloat32Slice(key string) []float32

MustGetFloat32Slice extracts slice of float32 value with the format "1.2,2.3,3.4" from env. if not set, it panics.

func MustGetFloat64 added in v1.1.0

func MustGetFloat64(key string) float64

MustGetFloat64 extracts int value from env. if not set, it panics.

func MustGetFloat64Slice added in v1.5.0

func MustGetFloat64Slice(key string) []float64

MustGetFloat64Slice extracts slice of float64 value with the format "1.2,2.3,3.4" from env. if not set, it panics.

func MustGetInt

func MustGetInt(key string) int

MustGetInt extracts int value from env. if not set, it panics.

func MustGetInt16 added in v1.1.0

func MustGetInt16(key string) int16

MustGetInt16 extracts int16 value from env. if not set, it panics.

func MustGetInt16Slice added in v1.4.0

func MustGetInt16Slice(key string) []int16

MustGetInt16Slice extracts slice of int16 value with the format "1,2,3" from env. if not set, it panics.

func MustGetInt32 added in v1.1.0

func MustGetInt32(key string) int32

MustGetInt32 extracts int32 value from env. if not set, it panics.

func MustGetInt32Slice added in v1.4.0

func MustGetInt32Slice(key string) []int32

MustGetInt32Slice extracts slice of int32 value with the format "1,2,3" from env. if not set, it panics.

func MustGetInt64 added in v1.1.0

func MustGetInt64(key string) int64

MustGetInt64 extracts int64 value from env. if not set, it panics.

func MustGetInt64Slice added in v1.4.0

func MustGetInt64Slice(key string) []int64

MustGetInt64Slice extracts slice of int64 value with the format "1,2,3" from env. if not set, it panics.

func MustGetInt8 added in v1.1.0

func MustGetInt8(key string) int8

MustGetInt8 extracts int8 value from env. if not set, it panics.

func MustGetInt8Slice added in v1.4.0

func MustGetInt8Slice(key string) []int8

MustGetInt8Slice extracts slice of int8 value with the format "1,2,3" from env. if not set, it panics.

func MustGetIntSlice added in v1.4.0

func MustGetIntSlice(key string) []int

MustGetIntSlice extracts slice of int value with the format "1,2,3" from env. if not set, it panics.

func MustGetString

func MustGetString(key string) string

MustGetString extracts string value from env. if not set, it panics.

func MustGetStringSlice added in v1.2.0

func MustGetStringSlice(key string) []string

MustGetStringSlice extracts slice of strings value with format "foo,bar,baz" from env. if not set, it panics.

func MustGetUint added in v1.4.0

func MustGetUint(key string) uint

MustGetUint extracts uint value from env. if not set, it panics.

func MustGetUint16 added in v1.4.0

func MustGetUint16(key string) uint16

MustGetUint16 extracts uint16 value from env. if not set, it panics.

func MustGetUint16Slice added in v1.4.0

func MustGetUint16Slice(key string) []uint16

MustGetUint16Slice extracts slice of uint16 value with the format "1,2,3" from env. if not set, it panics.

func MustGetUint32 added in v1.4.0

func MustGetUint32(key string) uint32

MustGetUint32 extracts uint32 value from env. if not set, it panics.

func MustGetUint32Slice added in v1.4.0

func MustGetUint32Slice(key string) []uint32

MustGetUint32Slice extracts slice of uint32 value with the format "1,2,3" from env. if not set, it panics.

func MustGetUint64 added in v1.4.0

func MustGetUint64(key string) uint64

MustGetUint64 extracts uint64 value from env. if not set, it panics.

func MustGetUint64Slice added in v1.4.0

func MustGetUint64Slice(key string) []uint64

MustGetUint64Slice extracts slice of uint64 value with the format "1,2,3" from env. if not set, it panics.

func MustGetUint8 added in v1.4.0

func MustGetUint8(key string) uint8

MustGetUint8 extracts uint8 value from env. if not set, it panics.

func MustGetUint8Slice added in v1.4.0

func MustGetUint8Slice(key string) []uint8

MustGetUint8Slice extracts slice of uint8 value with the format "1,2,3" from env. if not set, it panics.

func MustGetUintSlice added in v1.4.0

func MustGetUintSlice(key string) []uint

MustGetUintSlice extracts slice of uint value with the format "1,2,3" from env. if not set, it panics.

Types

type Env

type Env string

Env type.

const (
	Development Env = "development"
	Testing     Env = "testing"
	Staging     Env = "staging"
	Production  Env = "production"
)

Popular environments.

func Environment

func Environment() Env

Environment returns ENV value in environment variables.

type Type added in v1.6.0

type Type interface {
	bool |
		float32 | []float32 | float64 | []float64 |
		int | []int | int8 | []int8 | int16 | []int16 | int32 | []int32 | int64 | []int64 |
		string | []string |
		uint | []uint | uint8 | []uint8 | uint16 | []uint16 | uint32 | []uint32 | uint64 | []uint64
}

Jump to

Keyboard shortcuts

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