Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var (
ErrNotStruct = errors.New("target must be a struct or a pointer to a struct")
)
Functions ¶
func Marshal ¶
Marshal collects exported keys, values and comments (tag values) of a struct and puts them in a Go file with constant strings.
Example ¶
a := struct {
Foo string `
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.`
Hello string
Bool bool
Number int
Uint uint32
Float float64
}{
"Bar", "World", true, 123, 22, 1.23,
}
c, err := Marshal(a)
if err != nil {
panic(err)
}
fmt.Println(string(c))
b, err := json.MarshalIndent(ToConfigs(a), "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(b))
Output: package config const ( // Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor // incididunt ut labore et dolore magna aliqua. Foo = "Bar" Hello = "World" Bool = true Number = 123 Uint = 22 Float = 1.23 ) [ { "Key": "Foo", "Value": "Bar", "Comment": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor\nincididunt ut labore et dolore magna aliqua." }, { "Key": "Hello", "Value": "World", "Comment": "" }, { "Key": "Bool", "Value": "true", "Comment": "" }, { "Key": "Number", "Value": "123", "Comment": "" }, { "Key": "Uint", "Value": "22", "Comment": "" }, { "Key": "Float", "Value": "1.23", "Comment": "" } ]
func Unmarshal ¶
Unmarshal gets all constants in a Go file and assign them to the struct provided.
Example ¶
var c struct {
Foo string
Hello string
Bool bool
Number int
Uint uint32
Float float64
}
fmt.Printf("before: %+v\n", c)
err := Unmarshal([]byte(`
package config
const Foo = "BAR"
const (
Hello = "WORLD"
Bool = true
Number = 123
Uint = 22
Float = 1.23
)
`), &c)
if err != nil {
panic(err)
}
fmt.Printf("after: %+v\n", c)
Output: before: {Foo: Hello: Bool:false Number:0 Uint:0 Float:0} after: {Foo:BAR Hello:WORLD Bool:true Number:123 Uint:22 Float:1.23}
Types ¶
type CanSetString ¶ added in v1.2.0
Click to show internal directories.
Click to hide internal directories.