example_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2013 Dave Collins <dave@davec.name>
  3. *
  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.
  7. *
  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.
  15. */
  16. package spew_test
  17. import (
  18. "fmt"
  19. "github.com/davecgh/go-spew/spew"
  20. )
  21. type Flag int
  22. const (
  23. flagOne Flag = iota
  24. flagTwo
  25. )
  26. var flagStrings = map[Flag]string{
  27. flagOne: "flagOne",
  28. flagTwo: "flagTwo",
  29. }
  30. func (f Flag) String() string {
  31. if s, ok := flagStrings[f]; ok {
  32. return s
  33. }
  34. return fmt.Sprintf("Unknown flag (%d)", int(f))
  35. }
  36. type Bar struct {
  37. flag Flag
  38. data uintptr
  39. }
  40. type Foo struct {
  41. unexportedField Bar
  42. ExportedField map[interface{}]interface{}
  43. }
  44. // This example demonstrates how to use Dump to dump variables to stdout.
  45. func ExampleDump() {
  46. // The following package level declarations are assumed for this example:
  47. /*
  48. type Flag int
  49. const (
  50. flagOne Flag = iota
  51. flagTwo
  52. )
  53. var flagStrings = map[Flag]string{
  54. flagOne: "flagOne",
  55. flagTwo: "flagTwo",
  56. }
  57. func (f Flag) String() string {
  58. if s, ok := flagStrings[f]; ok {
  59. return s
  60. }
  61. return fmt.Sprintf("Unknown flag (%d)", int(f))
  62. }
  63. type Bar struct {
  64. flag Flag
  65. data uintptr
  66. }
  67. type Foo struct {
  68. unexportedField Bar
  69. ExportedField map[interface{}]interface{}
  70. }
  71. */
  72. // Setup some sample data structures for the example.
  73. bar := Bar{Flag(flagTwo), uintptr(0)}
  74. s1 := Foo{bar, map[interface{}]interface{}{"one": true}}
  75. f := Flag(5)
  76. // Dump!
  77. spew.Dump(s1, f)
  78. // Output:
  79. // (spew_test.Foo) {
  80. // unexportedField: (spew_test.Bar) {
  81. // flag: (spew_test.Flag) flagTwo,
  82. // data: (uintptr) <nil>
  83. // },
  84. // ExportedField: (map[interface {}]interface {}) {
  85. // (string) "one": (bool) true
  86. // }
  87. // }
  88. // (spew_test.Flag) Unknown flag (5)
  89. //
  90. }
  91. // This example demonstrates how to use Printf to display a variable with a
  92. // format string and inline formatting.
  93. func ExamplePrintf() {
  94. // Create a double pointer to a uint 8.
  95. ui8 := uint8(5)
  96. pui8 := &ui8
  97. ppui8 := &pui8
  98. // Create a circular data type.
  99. type circular struct {
  100. ui8 uint8
  101. c *circular
  102. }
  103. c := circular{ui8: 1}
  104. c.c = &c
  105. // Print!
  106. spew.Printf("ppui8: %v\n", ppui8)
  107. spew.Printf("circular: %v\n", c)
  108. // Output:
  109. // ppui8: <**>5
  110. // circular: {1 <*>{1 <*><shown>}}
  111. }
  112. // This example demonstrates how to use a SpewState.
  113. func ExampleSpewState() {
  114. // A SpewState does not need initialization.
  115. ss := new(spew.SpewState) // or var ss spew.SpewState
  116. // Modify the indent level of the SpewState only. The global configuration
  117. // is not modified.
  118. ssc := ss.Config()
  119. ssc.Indent = "\t"
  120. // Output using the SpewState instance.
  121. v := map[string]int{"one": 1}
  122. ss.Printf("v: %v\n", v)
  123. ss.Dump(v)
  124. // Output:
  125. // v: map[one:1]
  126. // (map[string]int) {
  127. // (string) "one": (int) 1
  128. // }
  129. }
  130. // This example demonstrates how to use a SpewState.Dump to dump variables to
  131. // stdout
  132. func ExampleSpewState_Dump() {
  133. // See the top-level Dump example for details on the types used in this
  134. // example.
  135. // A SpewState does not need initialization.
  136. ss := new(spew.SpewState) // or var ss spew.SpewState
  137. ss2 := new(spew.SpewState) // or var ss2 spew.SpewState
  138. // Modify the indent level of the first SpewState only.
  139. ssc := ss.Config()
  140. ssc.Indent = "\t"
  141. // Setup some sample data structures for the example.
  142. bar := Bar{Flag(flagTwo), uintptr(0)}
  143. s1 := Foo{bar, map[interface{}]interface{}{"one": true}}
  144. // Dump using the SpewState instances.
  145. ss.Dump(s1)
  146. ss2.Dump(s1)
  147. // Output:
  148. // (spew_test.Foo) {
  149. // unexportedField: (spew_test.Bar) {
  150. // flag: (spew_test.Flag) flagTwo,
  151. // data: (uintptr) <nil>
  152. // },
  153. // ExportedField: (map[interface {}]interface {}) {
  154. // (string) "one": (bool) true
  155. // }
  156. // }
  157. // (spew_test.Foo) {
  158. // unexportedField: (spew_test.Bar) {
  159. // flag: (spew_test.Flag) flagTwo,
  160. // data: (uintptr) <nil>
  161. // },
  162. // ExportedField: (map[interface {}]interface {}) {
  163. // (string) "one": (bool) true
  164. // }
  165. // }
  166. //
  167. }
  168. // This example demonstrates how to use SpewState.Printf to display a variable
  169. // with a format string and inline formatting.
  170. func ExampleSpewState_Printf() {
  171. // See the top-level Dump example for details on the types used in this
  172. // example.
  173. // A SpewState does not need initialization.
  174. ss := new(spew.SpewState) // or var ss spew.SpewState
  175. ss2 := new(spew.SpewState) // or var ss2 spew.SpewState
  176. // Modify the method handling of the first SpewState only.
  177. ssc := ss.Config()
  178. ssc.DisableMethods = true
  179. // This is of type Flag which implements a Stringer and has raw value 1.
  180. f := flagTwo
  181. // Dump using the SpewState instances.
  182. ss.Printf("f: %v\n", f)
  183. ss2.Printf("f: %v\n", f)
  184. // Output:
  185. // f: 1
  186. // f: flagTwo
  187. }