example_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. }