Fetching primitive values from env without hassle in Go!
Have you been annoyed when your code become ugly because you need to fetch value other than string from env but you also need to take care of its parsing error yet the only thing you need is just the value?
Here is an example of such situation when you are trying to fetch int
value:
...
strVal := os.Getenv("IntKey")
intVal, _ := strconv.Atoi(strVal) // too much hassle!
...
This module is offering you solution to that!
intVal := env.GetInt("IntKey") // so simple!
go get github.com/riandyrn/go-env
...
import "github.com/riandyrn/go-env"
...
strVal := env.GetString("StrKey") // fetch string value
intVal := env.GetInt("IntKey") // fetch int value
boolVal := env.GetBool("BoolKey") // fetch bool value
secVal := env.GetSeconds("SecondsKey") // fetch duration value in seconds
strsVal := env.GetStrings("StringsKey") // fetch slice of strings value
...
No need to care that troublesome errors!
All errors in the process would simply make the functions return their returned type zero value (e.g env.GetInt("abc")
would returns 0
).
Check env_test.go
to see possible scenarios handled by this module.
For full working example, check out /example
directory.