Skip to content

A simple golang library to get values from environment variables

License

Notifications You must be signed in to change notification settings

allisson/go-env

Repository files navigation

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

About

A simple golang library to get values from environment variables

Resources

License

Stars

Watchers

Forks

Packages

No packages published