mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
Add example of marshalling to string (#531)
This commit is contained in:
parent
15738813a0
commit
fb7a06477f
1 changed files with 22 additions and 0 deletions
22
README.md
22
README.md
|
@ -437,6 +437,7 @@ The following functions and methods exist:
|
|||
* `GetTime(key string) : time.Time`
|
||||
* `GetDuration(key string) : time.Duration`
|
||||
* `IsSet(key string) : bool`
|
||||
* `AllSettings() : map[string]interface{}`
|
||||
|
||||
One important thing to recognize is that each Get function will return a zero
|
||||
value if it’s not found. To check if a given key exists, the `IsSet()` method
|
||||
|
@ -590,6 +591,27 @@ if err != nil {
|
|||
}
|
||||
```
|
||||
|
||||
### Marshalling to string
|
||||
|
||||
You may need to marhsal all the settings held in viper into a string rather than write them to a file.
|
||||
You can use your favorite format's marshaller with the config returned by `AllSettings()`.
|
||||
|
||||
```go
|
||||
import (
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
// ...
|
||||
)
|
||||
|
||||
func yamlStringSettings() string {
|
||||
c := viper.AllSettings()
|
||||
bs, err := yaml.Marshal(c)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to marshal config to YAML: %v", err)
|
||||
}
|
||||
return string(bs)
|
||||
}
|
||||
```
|
||||
|
||||
## Viper or Vipers?
|
||||
|
||||
Viper comes ready to use out of the box. There is no configuration or
|
||||
|
|
Loading…
Reference in a new issue