From 034a2a5a5e92d7264ec7558d20f4d7746506ee8f Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sat, 12 Jan 2013 12:06:59 -0600 Subject: Implement support for unqiue config instances. This commit adds a new type, SpewState, which can be used to create instances with unique configuration options. The methods of SpewState are equivalent to the top-level functions. Full documentation and examples are included. --- spew/example_test.go | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) (limited to 'spew/example_test.go') diff --git a/spew/example_test.go b/spew/example_test.go index e96d2c1..2a35010 100644 --- a/spew/example_test.go +++ b/spew/example_test.go @@ -130,3 +130,95 @@ func ExamplePrintf() { // ppui8: <**>5 // circular: {1 <*>{1 <*>}} } + +// 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 + + // 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. + v := map[string]int{"one": 1} + ss.Printf("v: %v\n", v) + ss.Dump(v) + + // Output: + // v: map[one:1] + // (map[string]int) { + // (string) "one": (int) 1 + // } +} + +// This example demonstrates how to use a SpewState.Dump to dump variables to +// stdout +func ExampleSpewState_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" + + // 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) + + // Output: + // (spew_test.Foo) { + // unexportedField: (spew_test.Bar) { + // flag: (spew_test.Flag) flagTwo, + // data: (uintptr) + // }, + // ExportedField: (map[interface {}]interface {}) { + // (string) "one": (bool) true + // } + // } + // (spew_test.Foo) { + // unexportedField: (spew_test.Bar) { + // flag: (spew_test.Flag) flagTwo, + // data: (uintptr) + // }, + // ExportedField: (map[interface {}]interface {}) { + // (string) "one": (bool) true + // } + // } + // +} + +// This example demonstrates how to use SpewState.Printf to display a variable +// with a format string and inline formatting. +func ExampleSpewState_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 + + // Modify the method handling of the first SpewState only. + ssc := ss.Config() + ssc.DisableMethods = true + + // 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) + + // Output: + // f: 1 + // f: flagTwo +} -- cgit v1.2.3