Product Promotion
0x5a.live
for different kinds of informations and explorations.
GitHub - diegomarangoni/typenv: Go minimalist typed environment variables library
Go minimalist typed environment variables library. Contribute to diegomarangoni/typenv development by creating an account on GitHub.
Visit SiteGitHub - diegomarangoni/typenv: Go minimalist typed environment variables library
Go minimalist typed environment variables library. Contribute to diegomarangoni/typenv development by creating an account on GitHub.
Powered by 0x5a.live ๐
Typenv
Typenv is a minimalistic, zero dependency, typed environment variables library. It does support the following types:
- Bool
- Duration
- Float64
- Float32
- Int64
- Int32
- Int8
- Int
- String
Why typenv?
Handling environment variables in other type than strings or even handling fallback values is not a simple task, typenv makes easy to handle string, boolean, integer, float and time duration environment variables and also allows you to easily set default values for then.
How to use it
Basic usage
Go get the library:
go get diegomarangoni.dev/typenv
And use it:
if typenv.Bool("DEBUG") {
// do something
}
If the environment is NOT set, the zero value of the type will return, in this case false
.
With default value
You can fallback to a default value in case the environment variable is not set.
if typenv.Bool("DEBUG", true) {
// do something
}
If the environment is NOT set, the return value will be true
.
With global default value
You can also set a global default value for a environment. Useful when you use the same environment with same default value several times.
func init() {
typenv.SetGlobalDefault(
typenv.E(typenv.Bool, "DEBUG", true),
)
}
...
if typenv.Bool("DEBUG") {
// do something
}
If the environment is NOT set, the return value will be true
.
Overriding global default value
You still can override the global default value by setting a local default.
func init() {
typenv.SetGlobalDefault(
typenv.E(typenv.Bool, "DEBUG", true),
)
}
...
if typenv.Bool("DEBUG", false) {
// do something
}
If environment is NOT set, the return value will be false
, even if global default is telling that is true
.
GoLang Resources
are all listed below.
Made with โค๏ธ
to provide different kinds of informations and resources.