2 * Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 "github.com/davecgh/go-spew/spew"
32 var flagStrings = map[Flag]string{
37 func (f Flag) String() string {
38 if s, ok := flagStrings[f]; ok {
41 return fmt.Sprintf("Unknown flag (%d)", int(f))
50 ExportedField map[interface{}]interface{}
53 // This example demonstrates how to use Dump to dump variables to stdout.
55 // The following package level declarations are assumed for this example:
64 var flagStrings = map[Flag]string{
69 func (f Flag) String() string {
70 if s, ok := flagStrings[f]; ok {
73 return fmt.Sprintf("Unknown flag (%d)", int(f))
82 ExportedField map[interface{}]interface{}
86 // Setup some sample data structures for the example.
87 bar := Bar{uintptr(0)}
88 s1 := Foo{bar, map[interface{}]interface{}{"one": true}}
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,
103 // unexportedField: (spew_test.Bar) {
104 // data: (uintptr) <nil>
106 // ExportedField: (map[interface {}]interface {}) (len=1) {
107 // (string) (len=3) "one": (bool) true
110 // (spew_test.Flag) Unknown flag (5)
111 // ([]uint8) (len=34 cap=34) {
112 // 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... |
113 // 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0|
114 // 00000020 31 32 |12|
119 // This example demonstrates how to use Printf to display a variable with a
120 // format string and inline formatting.
121 func ExamplePrintf() {
122 // Create a double pointer to a uint 8.
127 // Create a circular data type.
128 type circular struct {
132 c := circular{ui8: 1}
136 spew.Printf("ppui8: %v\n", ppui8)
137 spew.Printf("circular: %v\n", c)
141 // circular: {1 <*>{1 <*><shown>}}
144 // This example demonstrates how to use a ConfigState.
145 func ExampleConfigState() {
146 // Modify the indent level of the ConfigState only. The global
147 // configuration is not modified.
148 scs := spew.ConfigState{Indent: "\t"}
150 // Output using the ConfigState instance.
151 v := map[string]int{"one": 1}
152 scs.Printf("v: %v\n", v)
157 // (map[string]int) (len=1) {
158 // (string) (len=3) "one": (int) 1
162 // This example demonstrates how to use ConfigState.Dump to dump variables to
164 func ExampleConfigState_Dump() {
165 // See the top-level Dump example for details on the types used in this
168 // Create two ConfigState instances with different indentation.
169 scs := spew.ConfigState{Indent: "\t"}
170 scs2 := spew.ConfigState{Indent: " "}
172 // Setup some sample data structures for the example.
173 bar := Bar{uintptr(0)}
174 s1 := Foo{bar, map[interface{}]interface{}{"one": true}}
176 // Dump using the ConfigState instances.
182 // unexportedField: (spew_test.Bar) {
183 // data: (uintptr) <nil>
185 // ExportedField: (map[interface {}]interface {}) (len=1) {
186 // (string) (len=3) "one": (bool) true
190 // unexportedField: (spew_test.Bar) {
191 // data: (uintptr) <nil>
193 // ExportedField: (map[interface {}]interface {}) (len=1) {
194 // (string) (len=3) "one": (bool) true
200 // This example demonstrates how to use ConfigState.Printf to display a variable
201 // with a format string and inline formatting.
202 func ExampleConfigState_Printf() {
203 // See the top-level Dump example for details on the types used in this
206 // Create two ConfigState instances and modify the method handling of the
207 // first ConfigState only.
208 scs := spew.NewDefaultConfig()
209 scs2 := spew.NewDefaultConfig()
210 scs.DisableMethods = true
213 // scs := spew.ConfigState{Indent: " ", DisableMethods: true}
214 // scs2 := spew.ConfigState{Indent: " "}
216 // This is of type Flag which implements a Stringer and has raw value 1.
219 // Dump using the ConfigState instances.
220 scs.Printf("f: %v\n", f)
221 scs2.Printf("f: %v\n", f)