This is a very simple library to read environment variables and parse them to multiple types.
To install the library in your project, run:
go get github.com/AgustinSRG/genv
This library provides several functions that get environment variables and automatically parse them to different types. You can also set default values in case the variable is empty or missing.
Here is en example usage
package main
import (
"fmt"
// Import the module
"github.com/AgustinSRG/genv"
)
func main() {
// You can get string variables
// If not set, you get the default value, passed as the second argument
boolVar := genv.GetEnvBool("TEST_STR_VAR", "default value")
fmt.Printf("TEST_STR_VAR = %v\n", boolVar)
// You can parse variables into boolean
// If they are set to TRUE or YES, you get true.
// If they are set to FALSE or NO, you get false.
// If they are not set to any of the above, you get the default value, passed as the second argument
strVar := genv.GetEnvBool("TEST_BOOL_VAR", false)
fmt.Printf("Parsed TEST_BOOL_VAR = %v\n", strVar)
// You can parse variables into integer
// If not set, or invalid, you get the default value, passed as the second argument
port := genv.GetEnvInt("PORT", 80)
fmt.Printf("PORT = %v\n", port)
// For types like integers, where the value can be invalid,
// you can use the alternative functions ending with "WithWarning"
// They do the same, but also return a boolean flag, set to true
// only if the value is set, but invalid
port, warning := genv.GetEnvIntWithWarning("PORT", 80)
if warning {
fmt.Println("[Warning] PORT has an invalid integer value, using the default value.")
}
fmt.Printf("PORT = %v\n", port)
// Same for the rest of the types. Check the documentation for more
}
To install dependencies, run:
go get .
To build the code, run:
go build .
To run the code linter, run:
golangci-lint run
In order to run the tests for this library, run:
go test -v