Go-Dump is a package which helps you to dump a struct to SdtOut
, any io.Writer
, or a map[string]string
.
type T struct {
A int
B string
}
a := T{23, "foo bar"}
dump.Fdump(out, a)
Will print
T.A: 23
T.B: foo bar
type T struct {
A int
B string
}
a := T{23, "foo bar"}
m, _ := dump.ToMap(a)
Will return such a map:
KEY | Value |
---|---|
T.A | 23 |
T.B | foo bar |
dump.ToMap(a, dump.WithDefaultLowerCaseFormatter())
type MyStruct struct {
A string
B struct {
InsideB string
}
}
var myStruct MyStruct
myStruct.A = "value A"
myStruct.B.InsideB = "value B"
dumper := dump.NewDefaultEncoder()
dumper.DisableTypePrefix = true
dumper.Separator = "_"
dumper.Prefix = "MYSTRUCT"
dumper.Formatters = []dump.KeyFormatterFunc{dump.WithDefaultUpperCaseFormatter()}
envs, _ := dumper.ToStringMap(&myStruct) // envs is the map of the dumped MyStruct
Will return such a map:
KEY | Value |
---|---|
MYSTRUCT_A | value A |
MYSTRUCT_B_INSIDEB | value B |
The environement variables can be handled by viper spf13/viper.
...
for k := range envs {
viper.BindEnv(dumper.ViperKey(k), k)
}
...
viperSettings := viper.AllSettings()
for k, v := range viperSettings {
fmt.Println(k, v)
}
...
See unit tests for more examples.
Go-Dump needs Go >= 1.8
No external dependencies :)