env

package module
Version: v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2020 License: MIT Imports: 4 Imported by: 4

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

Types

This section is empty.

Source Files

Jump to

Keyboard shortcuts

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