summaryrefslogtreecommitdiff
path: root/spew/example_test.go
diff options
context:
space:
mode:
authorDave Collins <[email protected]>2013-01-19 19:00:25 -0600
committerDave Collins <[email protected]>2013-01-19 19:00:25 -0600
commit13fc9b8d2f9442ecc1754b1738a97cf0a0754e92 (patch)
treed6a3ad0435a3fe54a62ef984b7b6d44e70c51f76 /spew/example_test.go
parent6026234f2b83bade0c5d18320806c81fd102b53f (diff)
Move SpewState methods to ConfigState.
Rather than stuffing a ConfigState instance into a separate SpewState, just add the functionality directly to the ConfigState. This provides simpler syntax for the consumer. One side effect of this change is that, unlike a zero value SpewState, a zero value ConfigState doesn't provide default values which means the Indent field is set to provide no indentation. The consumer is now expected to set the indent to their desired value when declaring an instance of ConfigState. Alternatively, the consumer can call a new function, NewDefaultConfig, which returns a ConfigState with default values, including a default indentation of a single space. For example, to change the indent to a tab, the previous syntax was: ss := new(spew.SpewState) // or var ss spew.SpewState scs := ss.Config() scs.Indent = "\t" scs.Dump(whatever) The new syntax is simply: scs := spew.ConfigState{Indent: "\t"} scs.Dump(whatever)
Diffstat (limited to 'spew/example_test.go')
-rw-r--r--spew/example_test.go64
1 files changed, 29 insertions, 35 deletions
diff --git a/spew/example_test.go b/spew/example_test.go
index 7a60202..acbfb2a 100644
--- a/spew/example_test.go
+++ b/spew/example_test.go
@@ -131,20 +131,16 @@ func ExamplePrintf() {
// circular: {1 <*>{1 <*><shown>}}
}
-// This example demonstrates how to use a SpewState.
-func ExampleSpewState() {
- // A SpewState does not need initialization.
- ss := new(spew.SpewState) // or var ss spew.SpewState
+// This example demonstrates how to use a ConfigState.
+func ExampleConfigState() {
+ // Modify the indent level of the ConfigState only. The global
+ // configuration is not modified.
+ scs := spew.ConfigState{Indent: "\t"}
- // Modify the indent level of the SpewState only. The global configuration
- // is not modified.
- ssc := ss.Config()
- ssc.Indent = "\t"
-
- // Output using the SpewState instance.
+ // Output using the ConfigState instance.
v := map[string]int{"one": 1}
- ss.Printf("v: %v\n", v)
- ss.Dump(v)
+ scs.Printf("v: %v\n", v)
+ scs.Dump(v)
// Output:
// v: map[one:1]
@@ -153,27 +149,23 @@ func ExampleSpewState() {
// }
}
-// This example demonstrates how to use a SpewState.Dump to dump variables to
+// This example demonstrates how to use ConfigState.Dump to dump variables to
// stdout
-func ExampleSpewState_Dump() {
+func ExampleConfigState_Dump() {
// See the top-level Dump example for details on the types used in this
// example.
- // A SpewState does not need initialization.
- ss := new(spew.SpewState) // or var ss spew.SpewState
- ss2 := new(spew.SpewState) // or var ss2 spew.SpewState
-
- // Modify the indent level of the first SpewState only.
- ssc := ss.Config()
- ssc.Indent = "\t"
+ // Create two ConfigState instances with different indentation.
+ scs := spew.ConfigState{Indent: "\t"}
+ scs2 := spew.ConfigState{Indent: " "}
// Setup some sample data structures for the example.
bar := Bar{Flag(flagTwo), uintptr(0)}
s1 := Foo{bar, map[interface{}]interface{}{"one": true}}
- // Dump using the SpewState instances.
- ss.Dump(s1)
- ss2.Dump(s1)
+ // Dump using the ConfigState instances.
+ scs.Dump(s1)
+ scs2.Dump(s1)
// Output:
// (spew_test.Foo) {
@@ -197,26 +189,28 @@ func ExampleSpewState_Dump() {
//
}
-// This example demonstrates how to use SpewState.Printf to display a variable
+// This example demonstrates how to use ConfigState.Printf to display a variable
// with a format string and inline formatting.
-func ExampleSpewState_Printf() {
+func ExampleConfigState_Printf() {
// See the top-level Dump example for details on the types used in this
// example.
- // A SpewState does not need initialization.
- ss := new(spew.SpewState) // or var ss spew.SpewState
- ss2 := new(spew.SpewState) // or var ss2 spew.SpewState
+ // Create two ConfigState instances and modify the method handling of the
+ // first ConfigState only.
+ scs := spew.NewDefaultConfig()
+ scs2 := spew.NewDefaultConfig()
+ scs.DisableMethods = true
- // Modify the method handling of the first SpewState only.
- ssc := ss.Config()
- ssc.DisableMethods = true
+ // Alternatively
+ // scs := spew.ConfigState{Indent: " ", DisableMethods: true}
+ // scs2 := spew.ConfigState{Indent: " "}
// This is of type Flag which implements a Stringer and has raw value 1.
f := flagTwo
- // Dump using the SpewState instances.
- ss.Printf("f: %v\n", f)
- ss2.Printf("f: %v\n", f)
+ // Dump using the ConfigState instances.
+ scs.Printf("f: %v\n", f)
+ scs2.Printf("f: %v\n", f)
// Output:
// f: 1