https://github.com/moznion/envs
a mapper of ENVironment variables to Structure for Go
https://github.com/moznion/envs
environment-variables go
Last synced: 8 months ago
JSON representation
a mapper of ENVironment variables to Structure for Go
- Host: GitHub
- URL: https://github.com/moznion/envs
- Owner: moznion
- License: mit
- Created: 2021-12-02T17:25:19.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-11-24T08:37:01.000Z (over 1 year ago)
- Last Synced: 2025-01-31T07:34:18.406Z (over 1 year ago)
- Topics: environment-variables, go
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# envs [](https://github.com/moznion/envs/actions/workflows/check.yml) [](https://codecov.io/gh/moznion/envs)
a mapper of ENVironment variables to a Structure for Go.
This library maps the environment variables to the struct according to the fields' types and tags.
Currently, it supports the following field types: string, int64, float64, bool, and the pointer for them.
## Synopsis
Basic usage:
```go
import (
"fmt"
"os"
"github.com/moznion/envs"
)
type StructuredEnv struct {
Foo string `envs:"FOO"`
Bar int64 `envs:"BAR"`
Buz float64 `envs:"BUZ"`
Qux bool `envs:"QUX"`
FooBar string `envs:"FOOBAR,allowempty"`
Nothing string
}
func main() {
_ = os.Setenv("FOO", "string-value")
_ = os.Setenv("BAR", "65535")
_ = os.Setenv("BUZ", "123.456")
_ = os.Setenv("QUX", "true")
var e StructuredEnv
err := envs.Unmarshal(&e)
if err != nil {
panic(err)
}
fmt.Printf("structured envvar:\n")
fmt.Printf(" Foo => \"%s\"\n", e.Foo)
fmt.Printf(" Bar => %d\n", e.Bar)
fmt.Printf(" Buz => %f\n", e.Buz)
fmt.Printf(" Qux => %v\n", e.Qux)
fmt.Printf(" FooBar => \"%s\"\n", e.FooBar)
fmt.Printf(" Nothing => \"%s\"\n", e.Nothing)
// Output:
// structured envvar:
// Foo => "string-value"
// Bar => 65535
// Buz => 123.456000
// Qux => true
// FooBar => ""
// Nothing => ""
}
```
Pointer based usage:
```go
import (
"fmt"
"os"
"github.com/moznion/envs"
)
type PtrStructuredEnv struct {
Foo *string `envs:"FOO"`
Bar *int64 `envs:"BAR"`
Buz *float64 `envs:"BUZ"`
Qux *bool `envs:"QUX"`
FooBar *string `envs:"FOOBAR,allowempty"`
Nothing *string
}
func main() {
_ = os.Setenv("FOO", "string-value")
_ = os.Setenv("BAR", "65535")
_ = os.Setenv("BUZ", "123.456")
_ = os.Setenv("QUX", "true")
var pe PtrStructuredEnv
err = envs.Unmarshal(&pe)
if err != nil {
panic(err)
}
fmt.Printf("pointer based structured envvar:\n")
fmt.Printf(" Foo => \"%s\"\n", *pe.Foo)
fmt.Printf(" Bar => %d\n", *pe.Bar)
fmt.Printf(" Buz => %f\n", *pe.Buz)
fmt.Printf(" Qux => %v\n", *pe.Qux)
fmt.Printf(" FooBar => %v\n", pe.FooBar)
fmt.Printf(" Nothing => %v\n", pe.Nothing)
// Output:
// pointer based structured envvar:
// Foo => "string-value"
// Bar => 65535
// Buz => 123.456000
// Qux => true
// FooBar =>
// Nothing =>
}
```
and examples are [here](./example_test.go)
## Documentations
[](https://godoc.org/github.com/moznion/envs)
## Author
moznion ()