Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
roeldev committed Apr 2, 2024
1 parent 197d51d commit 60ae23f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
51 changes: 45 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Included features are:
* Encoding environment variables from any type;
* Loading and overloading into the system's environment variables.

<hr>

```sh
go get github.com/go-pogo/env
```
Expand All @@ -42,12 +44,14 @@ import "github.com/go-pogo/env"

## Usage

Below example demonstrates how to decode system environment variables into a struct.

```go
package main

import (
"github.com/davecgh/go-spew/spew"
"github.com/go-pogo/env"
"log"
"time"
)

Expand All @@ -56,18 +60,53 @@ func main() {
Foo string
Timeout time.Duration `default:"10s"`
}

var conf Config
err := env.NewDecoder(env.System()).Decode(&conf)
if err != nil {
log.Fatalln("unable to decode system environment variables:", err)
if err := env.NewDecoder(env.System()).Decode(&conf); err != nil {
panic(err)
}
}

spew.Dump(conf)
// Output:
// (env.Config) {
// Foo: (string) "",
// Timeout: (time.Duration) 10s
// }
}
```

## Usage with `dotenv`

This example reads .env files from the _example_ directory and decodes the found variables into a struct.
```go
package main

import (
"github.com/davecgh/go-spew/spew"
"github.com/go-pogo/env"
"github.com/go-pogo/env/dotenv"
"time"
)

func main() {
type Config struct {
Foo string
Timeout time.Duration `default:"10s"`
}

var conf Config
if err := env.NewDecoder(dotenv.Read("example", dotenv.None)).Decode(&conf); err != nil {
panic(err)
}

spew.Dump(conf)
// Output:
// (dotenv.Config) {
// Foo: (string) (len=3) "bar",
// Timeout: (time.Duration) 2s
// }
}
```

## Documentation

Expand Down
10 changes: 5 additions & 5 deletions dotenv/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
Package dotenv supports reading and loading environment variables from .env
files based on active environment (e.g. prod, dev etc.). The order or reading
is as follows:
- .env
- .env.local
- .env.{active-env}
- .env.{active-env}.local
- .env
- .env.local
- .env.{active-env}
- .env.{active-env}.local
It is recommended to not commit any .local files to the repository as these
> It is recommended to not commit any .local files to the repository as these
represent variables that are specific to your local environment.
*/
package dotenv

0 comments on commit 60ae23f

Please sign in to comment.