From 38ea8fd0fcd119bb6230960477b85b389bda686a Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Sat, 26 Jul 2014 17:58:05 +0300 Subject: [PATCH] example: show examples in godoc --- structure_example_test.go | 60 +++++++++++++++++++++++++++++++++++++++ structure_test.go | 29 ------------------- 2 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 structure_example_test.go diff --git a/structure_example_test.go b/structure_example_test.go new file mode 100644 index 0000000..3c5e7bf --- /dev/null +++ b/structure_example_test.go @@ -0,0 +1,60 @@ +package structure + +import "fmt" + +func ExampleToMap() { + type Server struct { + Name string + ID int32 + Enabled bool + } + + s := &Server{ + Name: "Arslan", + ID: 123456, + Enabled: true, + } + + m, err := ToMap(s) + if err != nil { + panic(err) + } + + fmt.Printf("%#v\n", m["Name"]) + fmt.Printf("%#v\n", m["ID"]) + fmt.Printf("%#v\n", m["Enabled"]) + // Output: + // "Arslan" + // 123456 + // true + +} + +func ExampleToMap_tags() { + // Custom tags can change the map keys instead of using the fields name + type Server struct { + Name string `structure:"server_name"` + ID int32 `structure:"server_id"` + Enabled bool `structure:"enabled"` + } + + s := &Server{ + Name: "Zeynep", + ID: 789012, + } + + m, err := ToMap(s) + if err != nil { + panic(err) + } + + // access them by the custom tags defined above + fmt.Printf("%#v\n", m["server_name"]) + fmt.Printf("%#v\n", m["server_id"]) + fmt.Printf("%#v\n", m["enabled"]) + // Output: + // "Zeynep" + // 789012 + // false + +} diff --git a/structure_test.go b/structure_test.go index a556da3..057d0e0 100644 --- a/structure_test.go +++ b/structure_test.go @@ -1,7 +1,6 @@ package structure import ( - "fmt" "reflect" "testing" ) @@ -93,31 +92,3 @@ func TestToMap_Tag(t *testing.T) { } } - -func ExampleMap() { - type Server struct { - Name string - ID int32 - Enabled bool - } - - s := &Server{ - Name: "Arslan", - ID: 123456, - Enabled: true, - } - - m, err := ToMap(s) - if err != nil { - panic(err) - } - - fmt.Printf("%#v\n", m["Name"]) - fmt.Printf("%#v\n", m["ID"]) - fmt.Printf("%#v\n", m["Enabled"]) - // Output: - // "Arslan" - // 123456 - // true - -}