example_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. b := []byte{
  77. 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  78. 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
  79. 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
  80. 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
  81. 0x31, 0x32,
  82. }
  83. // Dump!
  84. spew.Dump(s1, f, b)
  85. // Output:
  86. // (spew_test.Foo) {
  87. // unexportedField: (spew_test.Bar) {
  88. // flag: (spew_test.Flag) flagTwo,
  89. // data: (uintptr) <nil>
  90. // },
  91. // ExportedField: (map[interface {}]interface {}) (len=1) {
  92. // (string) (len=3) "one": (bool) true
  93. // }
  94. // }
  95. // (spew_test.Flag) Unknown flag (5)
  96. // ([]uint8) (len=34 cap=34) {
  97. // 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... |
  98. // 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0|
  99. // 00000020 31 32 |12|
  100. // }
  101. //
  102. }
  103. // This example demonstrates how to use Printf to display a variable with a
  104. // format string and inline formatting.
  105. func ExamplePrintf() {
  106. // Create a double pointer to a uint 8.
  107. ui8 := uint8(5)
  108. pui8 := &ui8
  109. ppui8 := &pui8
  110. // Create a circular data type.
  111. type circular struct {
  112. ui8 uint8
  113. c *circular
  114. }
  115. c := circular{ui8: 1}
  116. c.c = &c
  117. // Print!
  118. spew.Printf("ppui8: %v\n", ppui8)
  119. spew.Printf("circular: %v\n", c)
  120. // Output:
  121. // ppui8: <**>5
  122. // circular: {1 <*>{1 <*><shown>}}
  123. }
  124. // This example demonstrates how to use a ConfigState.
  125. func ExampleConfigState() {
  126. // Modify the indent level of the ConfigState only. The global
  127. // configuration is not modified.
  128. scs := spew.ConfigState{Indent: "\t"}
  129. // Output using the ConfigState instance.
  130. v := map[string]int{"one": 1}
  131. scs.Printf("v: %v\n", v)
  132. scs.Dump(v)
  133. // Output:
  134. // v: map[one:1]
  135. // (map[string]int) (len=1) {
  136. // (string) (len=3) "one": (int) 1
  137. // }
  138. }
  139. // This example demonstrates how to use ConfigState.Dump to dump variables to
  140. // stdout
  141. func ExampleConfigState_Dump() {
  142. // See the top-level Dump example for details on the types used in this
  143. // example.
  144. // Create two ConfigState instances with different indentation.
  145. scs := spew.ConfigState{Indent: "\t"}
  146. scs2 := spew.ConfigState{Indent: " "}
  147. // Setup some sample data structures for the example.
  148. bar := Bar{Flag(flagTwo), uintptr(0)}
  149. s1 := Foo{bar, map[interface{}]interface{}{"one": true}}
  150. // Dump using the ConfigState instances.
  151. scs.Dump(s1)
  152. scs2.Dump(s1)
  153. // Output:
  154. // (spew_test.Foo) {
  155. // unexportedField: (spew_test.Bar) {
  156. // flag: (spew_test.Flag) flagTwo,
  157. // data: (uintptr) <nil>
  158. // },
  159. // ExportedField: (map[interface {}]interface {}) (len=1) {
  160. // (string) (len=3) "one": (bool) true
  161. // }
  162. // }
  163. // (spew_test.Foo) {
  164. // unexportedField: (spew_test.Bar) {
  165. // flag: (spew_test.Flag) flagTwo,
  166. // data: (uintptr) <nil>
  167. // },
  168. // ExportedField: (map[interface {}]interface {}) (len=1) {
  169. // (string) (len=3) "one": (bool) true
  170. // }
  171. // }
  172. //
  173. }
  174. // This example demonstrates how to use ConfigState.Printf to display a variable
  175. // with a format string and inline formatting.
  176. func ExampleConfigState_Printf() {
  177. // See the top-level Dump example for details on the types used in this
  178. // example.
  179. // Create two ConfigState instances and modify the method handling of the
  180. // first ConfigState only.
  181. scs := spew.NewDefaultConfig()
  182. scs2 := spew.NewDefaultConfig()
  183. scs.DisableMethods = true
  184. // Alternatively
  185. // scs := spew.ConfigState{Indent: " ", DisableMethods: true}
  186. // scs2 := spew.ConfigState{Indent: " "}
  187. // This is of type Flag which implements a Stringer and has raw value 1.
  188. f := flagTwo
  189. // Dump using the ConfigState instances.
  190. scs.Printf("f: %v\n", f)
  191. scs2.Printf("f: %v\n", f)
  192. // Output:
  193. // f: 1
  194. // f: flagTwo
  195. }