@@ -106,7 +106,7 @@ func main() {
106
106
var a , b config
107
107
108
108
// Update the environment.
109
- if err := env.Update (" ./cmd/. env" ); err != nil {
109
+ if err := env.Update (" .env" ); err != nil {
110
110
log.Fatal (err)
111
111
}
112
112
@@ -195,7 +195,7 @@ func main() {
195
195
// Note: We use the Load (but not Update) method for as not to overwrite
196
196
// the data in the environment because on the production server this
197
197
// data can be set forcibly.
198
- env.Load (" ./cmd/. env" )
198
+ env.Load (" .env" )
199
199
env.Unmarshal (" " , &config)
200
200
201
201
// Make routing.
@@ -472,6 +472,39 @@ marshaling.
472
472
// Port: 80
473
473
// AllowedHosts: 192.168.0.1
474
474
475
+ #### func Save
476
+
477
+ func Save(filename, prefix string, obj interface{}) error
478
+
479
+ Save saves the object to a file without changing the environment.
480
+
481
+
482
+ Example
483
+
484
+ There is some configuration structure:
485
+
486
+ // Config it's struct of the server configuration.
487
+ type Config struct {
488
+ Host string `env:"HOST"`
489
+ Port int `env:"PORT"`
490
+ AllowedHosts []string `env:"ALLOWED_HOSTS" sep:":"` // parse by `:`.
491
+ }
492
+
493
+ ...
494
+
495
+ var config = Config{
496
+ Host: "localhost",
497
+ Port: 8080,
498
+ AllowedHosts: []string{"localhost", "127.0.0.1"},
499
+ }
500
+ env.Save("/tmp/.env", "", config)
501
+
502
+ The result in the file /tmp/.env
503
+
504
+ HOST=localhost
505
+ PORT=8080
506
+ ALLOWED_HOSTS=localhost:127.0.0.1
507
+
475
508
#### func Set
476
509
477
510
func Set(key, value string) error
0 commit comments