Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/diegomarangoni/typenv
Go minimalist typed environment variables library
https://github.com/diegomarangoni/typenv
go golang library
Last synced: 14 days ago
JSON representation
Go minimalist typed environment variables library
- Host: GitHub
- URL: https://github.com/diegomarangoni/typenv
- Owner: diegomarangoni
- License: unlicense
- Created: 2020-06-30T18:26:09.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-19T13:49:20.000Z (almost 2 years ago)
- Last Synced: 2024-07-31T20:45:11.414Z (3 months ago)
- Topics: go, golang, library
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - typenv - Minimalistic, zero dependency, typed environment variables library. (Configuration / Standard CLI)
- fucking-awesome-go - typenv - Minimalistic, zero dependency, typed environment variables library. (Configuration / Standard CLI)
- awesome-go - typenv - Minimalistic, zero dependency, typed environment variables library. (Configuration / Standard CLI)
- awesome-go - typenv - Minimalistic, zero dependency, typed environment variables library. (Configuration / Standard CLI)
- awesome-go-extra - typenv - 06-30T18:26:09Z|2020-07-22T16:23:05Z| (Configuration / Advanced Console UIs)
- awesome-go-with-stars - typenv - Minimalistic, zero dependency, typed environment variables library. (Configuration / Standard CLI)
- awesome-go-plus - typenv - Minimalistic, zero dependency, typed environment variables library. (Configuration / Standard CLI)
- awesome-go-plus - typenv - Minimalistic, zero dependency, typed environment variables library. ![stars](https://img.shields.io/badge/stars-9-blue) ![forks](https://img.shields.io/badge/forks-1-blue) (Configuration / Standard CLI)
README
# Typenv
[![go.dev](https://img.shields.io/static/v1?label=go.dev&message=reference&color=00add8)](https://pkg.go.dev/diegomarangoni.dev/typenv) [![go report](https://goreportcard.com/badge/diegomarangoni.dev/typenv)](https://goreportcard.com/report/diegomarangoni.dev/typenv) [![codecov](https://codecov.io/gh/diegomarangoni/typenv/branch/master/graph/badge.svg)](https://codecov.io/gh/diegomarangoni/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:
```shell
go get diegomarangoni.dev/typenv
```And use it:
```go
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.
```go
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.```go
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.
```go
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`.