-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example to inject env into stuct fields
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2018 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package env_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/shaj13/env" | ||
) | ||
|
||
type Config struct { | ||
Host string | ||
Port string | ||
// .... | ||
} | ||
|
||
func Example_struct() { | ||
cfg := new(Config) | ||
|
||
es := env.NewEnvSet("app", env.ExitOnError) | ||
es.StringVar(&cfg.Host, "host", "localhost", "App host name") | ||
es.StringVar(&cfg.Port, "port", "443", "App port") | ||
|
||
es.Parse([]string{"APP_HOST=env.localhost"}) | ||
fmt.Printf(`%s:%s`, cfg.Host, cfg.Port) | ||
|
||
// Output: | ||
// env.localhost:443 | ||
} |