Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/agustinsrg/genv
Utility library to read and parse environment variables (Go)
https://github.com/agustinsrg/genv
environment-variables golang golang-library
Last synced: 24 days ago
JSON representation
Utility library to read and parse environment variables (Go)
- Host: GitHub
- URL: https://github.com/agustinsrg/genv
- Owner: AgustinSRG
- License: mit
- Created: 2024-11-28T17:30:24.000Z (25 days ago)
- Default Branch: master
- Last Pushed: 2024-11-28T17:32:49.000Z (25 days ago)
- Last Synced: 2024-11-28T18:33:19.397Z (25 days ago)
- Topics: environment-variables, golang, golang-library
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Utility library to read and parse environment variables (Go)
This is a very simple library to read environment variables and parse them to multiple types.
[Documentation](https://pkg.go.dev/github.com/AgustinSRG/genv)
## Installation
To install the library in your project, run:
```sh
go get https://github.com/AgustinSRG/genv
```## Usage
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
```go
package mainimport (
"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 := 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
}
```## Build the library
To install dependencies, run:
```sh
go get .
```To build the code, run:
```sh
go build .
```## Run linter
To run the code linter, run:
```sh
golangci-lint run
```## Run tests
In order to run the tests for this library, run:
```sh
go test -v
```