structs_example: add examples for omitempty usage
This commit is contained in:
parent
6dd7cda522
commit
4018983491
@ -111,6 +111,28 @@ func ExampleMap_nested() {
|
|||||||
// 2013-02-03 00:00:00 +0000 UTC
|
// 2013-02-03 00:00:00 +0000 UTC
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleMap_omitEmpty() {
|
||||||
|
// By default field with struct types of zero values are processed too. We
|
||||||
|
// can stop processing them via "omitempty" tag option.
|
||||||
|
type Server struct {
|
||||||
|
Name string `structs:",omitempty"`
|
||||||
|
ID int32 `structs:"server_id,omitempty"`
|
||||||
|
Location string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only add location
|
||||||
|
s := &Server{
|
||||||
|
Location: "Tokyo",
|
||||||
|
}
|
||||||
|
|
||||||
|
m := Map(s)
|
||||||
|
|
||||||
|
// map contains only the Location field
|
||||||
|
fmt.Printf("%v\n", m)
|
||||||
|
// Output:
|
||||||
|
// map[Location:Tokyo]
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleValues() {
|
func ExampleValues() {
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Name string
|
Name string
|
||||||
@ -131,6 +153,28 @@ func ExampleValues() {
|
|||||||
// Values: [Fatih 135790 false]
|
// Values: [Fatih 135790 false]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleValues_omitEmpty() {
|
||||||
|
// By default field with struct types of zero values are processed too. We
|
||||||
|
// can stop processing them via "omitempty" tag option.
|
||||||
|
type Server struct {
|
||||||
|
Name string `structs:",omitempty"`
|
||||||
|
ID int32 `structs:"server_id,omitempty"`
|
||||||
|
Location string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only add location
|
||||||
|
s := &Server{
|
||||||
|
Location: "Ankara",
|
||||||
|
}
|
||||||
|
|
||||||
|
m := Values(s)
|
||||||
|
|
||||||
|
// values contains only the Location field
|
||||||
|
fmt.Printf("Values: %+v\n", m)
|
||||||
|
// Output:
|
||||||
|
// Values: [Ankara]
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleValues_tags() {
|
func ExampleValues_tags() {
|
||||||
type Location struct {
|
type Location struct {
|
||||||
City string
|
City string
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user