example_test.go 5.4 KB

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