env

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2023 License: MIT Imports: 4 Imported by: 12

README

go-env

Build Status Go Report Card go.dev reference

A simple golang library to get values from environment variables.

Example:

package main

import (
	"fmt"
	"os"

	"github.com/allisson/go-env"
)

func main() {
	// Set a environment variable
	os.Setenv("STRING", "string")
	os.Setenv("STRING_SLICE", "string1,string2,string3")
	os.Setenv("DURATION", 10, time.Seconds)

	// Get a environment variable
	str := env.GetString("STRING", "default-value")
	fmt.Printf("str=%#v\n", str)

	// Get a not set environment variable (return default value as fallback)
	str2 := env.GetString("STRING2", "default-value-for-string2")
	fmt.Printf("str2=%#v\n", str2)

	// Get a slice environment variable
	str3 := env.GetStringSlice("STRING_SLICE", ",", []string{"default-value"})
	fmt.Printf("str3=%#v\n", str3)

	duration := env.GetDuration("DURATION", 1, time.Seconds)
	fmt.Printf("duration=%v\n", duration)

	// Other functions:
	// GetInt()
	// GetIntSlice()
	// GetInt32()
	// GetInt32Slice()
	// GetInt64()
	// GetInt64Slice()
	// GetBool()
	// GetBoolSlice()
	// GetFloat32()
	// GetFloat32Slice()
	// GetFloat64()
	// GetFloat64Slice()
	// GetBytes()
}
go run main.go
str="string"
str2="default-value-for-string2"
str3=[]string{"string1", "string2", "string3"}
duration=10h0m0s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBool

func GetBool(key string, defaultValue bool) bool

GetBool returns a boolean value from environment variable or default value

func GetBoolSlice added in v0.2.0

func GetBoolSlice(key, sep string, defaultValue []bool) []bool

GetBoolSlice returns a boolean slice from environment variable or default value

func GetBytes

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

GetBytes returns a byte slice value from environment variable or default value

func GetDuration added in v0.3.0

func GetDuration(key string, defaultValue int64, duration time.Duration) time.Duration

GetDuration returns a time.Duration value from environment variable or default value

func GetFloat32

func GetFloat32(key string, defaultValue float32) float32

GetFloat32 returns a float32 value from environment variable or default value

func GetFloat32Slice added in v0.2.0

func GetFloat32Slice(key, sep string, defaultValue []float32) []float32

GetFloat32Slice returns a float32 slice from environment variable or default value

func GetFloat64

func GetFloat64(key string, defaultValue float64) float64

GetFloat64 returns a float64 value from environment variable or default value

func GetFloat64Slice added in v0.2.0

func GetFloat64Slice(key, sep string, defaultValue []float64) []float64

GetFloat64Slice returns a float64 slice from environment variable or default value

func GetInt

func GetInt(key string, defaultValue int) int

GetInt returns a int value from environment variable or default value

func GetInt32

func GetInt32(key string, defaultValue int32) int32

GetInt32 returns a int32 value from environment variable or default value

func GetInt32Slice added in v0.2.0

func GetInt32Slice(key, sep string, defaultValue []int32) []int32

GetInt32Slice returns a int32 slice from environment variable or default value

func GetInt64

func GetInt64(key string, defaultValue int64) int64

GetInt64 returns a int64 value from environment variable or default value

func GetInt64Slice added in v0.2.0

func GetInt64Slice(key, sep string, defaultValue []int64) []int64

GetInt64Slice returns a int64 slice from environment variable or default value

func GetIntSlice added in v0.2.0

func GetIntSlice(key, sep string, defaultValue []int) []int

GetIntSlice returns a int slice from environment variable or default value

func GetString

func GetString(key, defaultValue string) string

GetString returns a string value from environment variable or default value

func GetStringSlice added in v0.2.0

func GetStringSlice(key, sep string, defaultValue []string) []string

GetStringSlice returns a string slice from environment variable or default value

func GetUint added in v0.4.0

func GetUint(key string, defaultValue uint) uint

GetUint returns a uint value from environment variable or default value

func GetUint32 added in v0.4.0

func GetUint32(key string, defaultValue uint32) uint32

GetUint32 returns a uint32 value from environment variable or default value

func GetUint32Slice added in v0.4.0

func GetUint32Slice(key, sep string, defaultValue []uint32) []uint32

GetUint32Slice returns a uint32 slice from environment variable or default value

func GetUint64 added in v0.4.0

func GetUint64(key string, defaultValue uint64) uint64

GetUint64 returns a uint64 value from environment variable or default value

func GetUint64Slice added in v0.4.0

func GetUint64Slice(key, sep string, defaultValue []uint64) []uint64

GetUint64Slice returns a uint64 slice from environment variable or default value

func GetUintSlice added in v0.4.0

func GetUintSlice(key, sep string, defaultValue []uint) []uint

GetUintSlice returns a uint slice from environment variable or default value

Types

This section is empty.

Jump to

Keyboard shortcuts

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