@@ -21,35 +21,36 @@ debugging.
2121A quick overview of the additional features spew provides over the built-in
2222printing facilities for Go data types are as follows:
2323
24- * Pointers are dereferenced and followed
25- * Circular data structures are detected and handled properly
26- * Custom Stringer/error interfaces are optionally invoked, including
27- on unexported types
28- * Custom types which only implement the Stringer/error interfaces via
29- a pointer receiver are optionally invoked when passing non-pointer
30- variables
31- * Byte arrays and slices are dumped like the hexdump -C command which
32- includes offsets, byte values in hex, and ASCII output (only when using
33- Dump style)
24+ - Pointers are dereferenced and followed
25+ - Circular data structures are detected and handled properly
26+ - Custom Stringer/error interfaces are optionally invoked, including
27+ on unexported types
28+ - Custom types which only implement the Stringer/error interfaces via
29+ a pointer receiver are optionally invoked when passing non-pointer
30+ variables
31+ - Byte arrays and slices are dumped like the hexdump -C command which
32+ includes offsets, byte values in hex, and ASCII output (only when using
33+ Dump style)
3434
3535There are two different approaches spew allows for dumping Go data structures:
3636
37- * Dump style which prints with newlines, customizable indentation,
38- and additional debug information such as types and all pointer addresses
39- used to indirect to the final value
40- * A custom Formatter interface that integrates cleanly with the standard fmt
41- package and replaces %v, %+v, %#v, and %#+v to provide inline printing
42- similar to the default %v while providing the additional functionality
43- outlined above and passing unsupported format verbs such as %x and %q
44- along to fmt
37+ - Dump style which prints with newlines, customizable indentation,
38+ and additional debug information such as types and all pointer addresses
39+ used to indirect to the final value
40+ - A custom Formatter interface that integrates cleanly with the standard fmt
41+ package and replaces %v, %+v, %#v, and %#+v to provide inline printing
42+ similar to the default %v while providing the additional functionality
43+ outlined above and passing unsupported format verbs such as %x and %q
44+ along to fmt
4545
46- Quick Start
46+ # Quick Start
4747
4848This section demonstrates how to quickly get started with spew. See the
4949sections below for further details on formatting and configuration options.
5050
5151To dump a variable with full newlines, indentation, type, and pointer
5252information use Dump, Fdump, or Sdump:
53+
5354 spew.Dump(myVar1, myVar2, ...)
5455 spew.Fdump(someWriter, myVar1, myVar2, ...)
5556 str := spew.Sdump(myVar1, myVar2, ...)
@@ -58,12 +59,13 @@ Alternatively, if you would prefer to use format strings with a compacted inline
5859printing style, use the convenience wrappers Printf, Fprintf, etc with
5960%v (most compact), %+v (adds pointer addresses), %#v (adds types), or
6061%#+v (adds types and pointer addresses):
62+
6163 spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2)
6264 spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
6365 spew.Fprintf(someWriter, "myVar1: %v -- myVar2: %+v", myVar1, myVar2)
6466 spew.Fprintf(someWriter, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
6567
66- Configuration Options
68+ # Configuration Options
6769
6870Configuration of spew is handled by fields in the ConfigState type. For
6971convenience, all of the top-level functions use a global state available
@@ -74,51 +76,52 @@ equivalent to the top-level functions. This allows concurrent configuration
7476options. See the ConfigState documentation for more details.
7577
7678The following configuration options are available:
77- * Indent
78- String to use for each indentation level for Dump functions.
79- It is a single space by default. A popular alternative is "\t".
80-
81- * MaxDepth
82- Maximum number of levels to descend into nested data structures.
83- There is no limit by default.
84-
85- * DisableMethods
86- Disables invocation of error and Stringer interface methods.
87- Method invocation is enabled by default.
88-
89- * DisablePointerMethods
90- Disables invocation of error and Stringer interface methods on types
91- which only accept pointer receivers from non-pointer variables.
92- Pointer method invocation is enabled by default.
93-
94- * DisablePointerAddresses
95- DisablePointerAddresses specifies whether to disable the printing of
96- pointer addresses. This is useful when diffing data structures in tests.
97-
98- * DisableCapacities
99- DisableCapacities specifies whether to disable the printing of
100- capacities for arrays, slices, maps and channels. This is useful when
101- diffing data structures in tests.
102-
103- * ContinueOnMethod
104- Enables recursion into types after invoking error and Stringer interface
105- methods. Recursion after method invocation is disabled by default.
106-
107- * SortKeys
108- Specifies map keys should be sorted before being printed. Use
109- this to have a more deterministic, diffable output. Note that
110- only native types (bool, int, uint, floats, uintptr and string)
111- and types which implement error or Stringer interfaces are
112- supported with other types sorted according to the
113- reflect.Value.String() output which guarantees display
114- stability. Natural map order is used by default.
115-
116- * SpewKeys
117- Specifies that, as a last resort attempt, map keys should be
118- spewed to strings and sorted by those strings. This is only
119- considered if SortKeys is true.
120-
121- Dump Usage
79+
80+ - Indent
81+ String to use for each indentation level for Dump functions.
82+ It is a single space by default. A popular alternative is "\t".
83+
84+ - MaxDepth
85+ Maximum number of levels to descend into nested data structures.
86+ There is no limit by default.
87+
88+ - DisableMethods
89+ Disables invocation of error and Stringer interface methods.
90+ Method invocation is enabled by default.
91+
92+ - DisablePointerMethods
93+ Disables invocation of error and Stringer interface methods on types
94+ which only accept pointer receivers from non-pointer variables.
95+ Pointer method invocation is enabled by default.
96+
97+ - DisablePointerAddresses
98+ DisablePointerAddresses specifies whether to disable the printing of
99+ pointer addresses. This is useful when diffing data structures in tests.
100+
101+ - DisableCapacities
102+ DisableCapacities specifies whether to disable the printing of
103+ capacities for arrays, slices, maps and channels. This is useful when
104+ diffing data structures in tests.
105+
106+ - ContinueOnMethod
107+ Enables recursion into types after invoking error and Stringer interface
108+ methods. Recursion after method invocation is disabled by default.
109+
110+ - SortKeys
111+ Specifies map keys should be sorted before being printed. Use
112+ this to have a more deterministic, diffable output. Note that
113+ only native types (bool, int, uint, floats, uintptr and string)
114+ and types which implement error or Stringer interfaces are
115+ supported with other types sorted according to the
116+ reflect.Value.String() output which guarantees display
117+ stability. Natural map order is used by default.
118+
119+ - SpewKeys
120+ Specifies that, as a last resort attempt, map keys should be
121+ spewed to strings and sorted by those strings. This is only
122+ considered if SortKeys is true.
123+
124+ # Dump Usage
122125
123126Simply call spew.Dump with a list of variables you want to dump:
124127
@@ -133,7 +136,7 @@ A third option is to call spew.Sdump to get the formatted output as a string:
133136
134137 str := spew.Sdump(myVar1, myVar2, ...)
135138
136- Sample Dump Output
139+ # Sample Dump Output
137140
138141See the Dump example for details on the setup of the types and variables being
139142shown here.
@@ -150,13 +153,14 @@ shown here.
150153
151154Byte (and uint8) arrays and slices are displayed uniquely like the hexdump -C
152155command as shown.
156+
153157 ([]uint8) (len=32 cap=32) {
154158 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... |
155159 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0|
156160 00000020 31 32 |12|
157161 }
158162
159- Custom Formatter
163+ # Custom Formatter
160164
161165Spew provides a custom formatter that implements the fmt.Formatter interface
162166so that it integrates cleanly with standard fmt package printing functions. The
@@ -170,7 +174,7 @@ standard fmt package for formatting. In addition, the custom formatter ignores
170174the width and precision arguments (however they will still work on the format
171175specifiers not handled by the custom formatter).
172176
173- Custom Formatter Usage
177+ # Custom Formatter Usage
174178
175179The simplest way to make use of the spew custom formatter is to call one of the
176180convenience functions such as spew.Printf, spew.Println, or spew.Printf. The
@@ -184,15 +188,17 @@ functions have syntax you are most likely already familiar with:
184188
185189See the Index for the full list convenience functions.
186190
187- Sample Formatter Output
191+ # Sample Formatter Output
188192
189193Double pointer to a uint8:
194+
190195 %v: <**>5
191196 %+v: <**>(0xf8400420d0->0xf8400420c8)5
192197 %#v: (**uint8)5
193198 %#+v: (**uint8)(0xf8400420d0->0xf8400420c8)5
194199
195200Pointer to circular struct with a uint8 field and a pointer to itself:
201+
196202 %v: <*>{1 <*><shown>}
197203 %+v: <*>(0xf84003e260){ui8:1 c:<*>(0xf84003e260)<shown>}
198204 %#v: (*main.circular){ui8:(uint8)1 c:(*main.circular)<shown>}
@@ -201,7 +207,7 @@ Pointer to circular struct with a uint8 field and a pointer to itself:
201207See the Printf example for details on the setup of variables being shown
202208here.
203209
204- Errors
210+ # Errors
205211
206212Since it is possible for custom Stringer/error interfaces to panic, spew
207213detects them and handles them internally by printing the panic information
0 commit comments