This example demonstrates how to use Dump to dump variables to stdout.
()
| 52 | |
| 53 | // This example demonstrates how to use Dump to dump variables to stdout. |
| 54 | func ExampleDump() { |
| 55 | // The following package level declarations are assumed for this example: |
| 56 | /* |
| 57 | type Flag int |
| 58 | |
| 59 | const ( |
| 60 | flagOne Flag = iota |
| 61 | flagTwo |
| 62 | ) |
| 63 | |
| 64 | var flagStrings = map[Flag]string{ |
| 65 | flagOne: "flagOne", |
| 66 | flagTwo: "flagTwo", |
| 67 | } |
| 68 | |
| 69 | func (f Flag) String() string { |
| 70 | if s, ok := flagStrings[f]; ok { |
| 71 | return s |
| 72 | } |
| 73 | return fmt.Sprintf("Unknown flag (%d)", int(f)) |
| 74 | } |
| 75 | |
| 76 | type Bar struct { |
| 77 | data uintptr |
| 78 | } |
| 79 | |
| 80 | type Foo struct { |
| 81 | unexportedField Bar |
| 82 | ExportedField map[interface{}]interface{} |
| 83 | } |
| 84 | */ |
| 85 | |
| 86 | // Setup some sample data structures for the example. |
| 87 | bar := Bar{uintptr(0)} |
| 88 | s1 := Foo{bar, map[interface{}]interface{}{"one": true}} |
| 89 | f := Flag(5) |
| 90 | b := []byte{ |
| 91 | 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, |
| 92 | 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, |
| 93 | 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, |
| 94 | 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, |
| 95 | 0x31, 0x32, |
| 96 | } |
| 97 | |
| 98 | // Dump! |
| 99 | spew.Dump(s1, f, b) |
| 100 | |
| 101 | // Output: |
| 102 | // (spew_test.Foo) { |
| 103 | // unexportedField: (spew_test.Bar) { |
| 104 | // data: (uintptr) <nil> |
| 105 | // }, |
| 106 | // ExportedField: (map[interface {}]interface {}) (len=1) { |
| 107 | // (string) (len=3) "one": (bool) true |
| 108 | // } |
| 109 | // } |
| 110 | // (spew_test.Flag) Unknown flag (5) |
| 111 | // ([]uint8) (len=34 cap=34) { |