Source file
src/fmt/gostringer_example_test.go
1
2
3
4
5 package fmt_test
6
7 import (
8 "fmt"
9 )
10
11
12 type Address struct {
13 City string
14 State string
15 Country string
16 }
17
18
19 type Person struct {
20 Name string
21 Age uint
22 Addr *Address
23 }
24
25
26
27 func (p Person) GoString() string {
28 if p.Addr != nil {
29 return fmt.Sprintf("Person{Name: %q, Age: %d, Addr: &Address{City: %q, State: %q, Country: %q}}", p.Name, int(p.Age), p.Addr.City, p.Addr.State, p.Addr.Country)
30 }
31 return fmt.Sprintf("Person{Name: %q, Age: %d}", p.Name, int(p.Age))
32 }
33
34 func ExampleGoStringer() {
35 p1 := Person{
36 Name: "Warren",
37 Age: 31,
38 Addr: &Address{
39 City: "Denver",
40 State: "CO",
41 Country: "U.S.A.",
42 },
43 }
44
45
46 fmt.Printf("%#v\n", p1)
47
48 p2 := Person{
49 Name: "Theia",
50 Age: 4,
51 }
52
53
54 fmt.Printf("%#v\n", p2)
55
56
57
58
59 }
60
View as plain text