aboutsummaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-02-14 11:40:09 +0100
committerPacien TRAN-GIRARD2015-02-14 11:44:31 +0100
commit96e522c8ed36b8888fbd33b7be98dca98ae9dc4a (patch)
treead97fb9c9e2013d67105bbeaf5ed49f8e7c97477 /README.md
parent451997aa1fd9f4150ea9f1dbc2d9e017e0866938 (diff)
downloadgo-envcfg-96e522c8ed36b8888fbd33b7be98dca98ae9dc4a.tar.gz
Add readmeHEADmaster
Diffstat (limited to 'README.md')
-rw-r--r--README.md94
1 files changed, 94 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..b61fe1c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,94 @@
1envcfg [![Build Status](https://travis-ci.org/Pacien/envcfg.svg)](//travis-ci.org/Pacien/envcfg)
2======
3
4Package [envcfg](//github.com/Pacien/envcfg) provides environment variable mapping to structs.
5
6It can be used to read configuration parameters from the environment.
7
8Fields for which environment variables can be found are overwritten, otherwise they are left to their previous value.
9
10This package can be used, for example, after [gcfg](//code.google.com/p/gcfg/) to override settings provided in a
11configuration file.
12
13
14```Go
15import "github.com/Pacien/envcfg"
16```
17
18
19Documentation
20-------------
21
22Package documentation can be found on [GoDoc](//godoc.org/github.com/Pacien/envcfg)
23
24
25Usage example
26-------------
27
28Set environment variables:
29
30```Bash
31export PORT=8080
32export USER_PASSWORD="S3cUrE"
33```
34
35
36Create a struct optionally with tagged fields and/or already set values, then call the `ReadInto` function to read
37the values set in the environment variables.
38
39```Go
40package main
41
42import (
43 "fmt"
44 "github.com/Pacien/envcfg"
45)
46
47type Config struct {
48 Server struct {
49 Port int `env:"PORT" absenv:"true"`
50 }
51 User struct {
52 Username string
53 Password string
54 }
55}
56
57var cnf Config
58
59func (c *Config) setDefaults() *Config {
60 c.User.Username = "root"
61 c.User.Password = "password"
62 return c
63}
64
65func init() {
66 cnf.setDefaults()
67
68 _, errs := envcfg.ReadInto(&cnf)
69 if len(errs) != 0 {
70 fmt.Println(errs)
71 }
72}
73
74func main() {
75 fmt.Println(cnf)
76}
77```
78
79
80Output of the previous program:
81
82```Bash
83{{8080} {root S3cUrE}}
84```
85
86
87See tests for other examples.
88
89
90License
91-------
92
93This program is published under the MIT License.
94See the LICENSE.txt file.