example: show examples in godoc

This commit is contained in:
Fatih Arslan 2014-07-26 17:58:05 +03:00
parent d38308cd6b
commit 38ea8fd0fc
2 changed files with 60 additions and 29 deletions

60
structure_example_test.go Normal file
View File

@ -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
}

View File

@ -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
}