doc.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. /*
  17. Package spew implements a deep pretty printer for Go data structures to aid in
  18. debugging.
  19. A quick overview of the additional features spew provides over the built-in
  20. printing facilities for Go data types are as follows:
  21. * Pointers are dereferenced and followed
  22. * Circular data structures are detected and handled properly
  23. * Custom Stringer/error interfaces are optionally invoked, including
  24. on unexported types
  25. * Custom types which only implement the Stringer/error interfaces via
  26. a pointer receiver are optionally invoked when passing non-pointer
  27. variables
  28. There are two different approaches spew allows for dumping Go data structures:
  29. * Dump style which prints with newlines, customizable indentation,
  30. and additional debug information such as types and all pointer addresses
  31. used to indirect to the final value
  32. * A custom Formatter interface that integrates cleanly with the standard fmt
  33. package and replaces %v, %+v, %#v, and %#+v to provide inline printing
  34. similar to the default %v while providing the additional functionality
  35. outlined above and passing unsupported format verbs such as %x and %q
  36. along to fmt
  37. Quick Start
  38. This section demonstrates how to quickly get started with spew. See the
  39. sections below for further details on formatting and configuration options.
  40. To dump a variable with full newlines, indentation, type, and pointer
  41. information use Dump or Fdump:
  42. spew.Dump(myVar1, myVar2, ...)
  43. spew.Fdump(someWriter, myVar1, myVar2, ...)
  44. Alternatively, if you would prefer to use format strings with a compacted inline
  45. printing style, use the convenience wrappers Printf, Fprintf, etc with
  46. %v (most compact), %+v (adds pointer addresses), %#v (adds types), or
  47. %#+v (adds types and pointer addresses):
  48. spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2)
  49. spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
  50. spew.Fprintf(someWriter, "myVar1: %v -- myVar2: %+v", myVar1, myVar2)
  51. spew.Fprintf(someWriter, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
  52. Configuration Options
  53. Configuration of spew is handled by fields in the ConfigState type. For
  54. convenience, all of the top-level functions use a global state available
  55. via the spew.Config global.
  56. It is also possible to create a ConfigState instance that provides methods
  57. equivalent to the top-level functions. This allows concurrent configuration
  58. options. See the ConfigState documentation for more details.
  59. The following configuration options are available:
  60. * Indent
  61. String to use for each indentation level for Dump functions.
  62. It is a single space by default. A popular alternative is "\t".
  63. * MaxDepth
  64. Maximum number of levels to descend into nested data structures.
  65. There is no limit by default.
  66. * DisableMethods
  67. Disables invocation of error and Stringer interface methods.
  68. Method invocation is enabled by default.
  69. * DisablePointerMethods
  70. Disables invocation of error and Stringer interface methods on types
  71. which only accept pointer receivers from non-pointer variables.
  72. Pointer method invocation is enabled by default.
  73. Dump Usage
  74. Simply call spew.Dump with a list of variables you want to dump:
  75. spew.Dump(myVar1, myVar2, ...)
  76. You may also call spew.Fdump if you would prefer to output to an arbitrary
  77. io.Writer. For example, to dump to standard error:
  78. spew.Fdump(os.Stderr, myVar1, myVar2, ...)
  79. Sample Dump Output
  80. See the Dump example for details on the setup of the types and variables being
  81. shown here.
  82. (main.Foo) {
  83. unexportedField: (*main.Bar)(0xf84002e210)({
  84. flag: (main.Flag) flagTwo,
  85. data: (uintptr) <nil>
  86. }),
  87. ExportedField: (map[interface {}]interface {}) {
  88. (string) "one": (bool) true
  89. }
  90. }
  91. Custom Formatter
  92. Spew provides a custom formatter the implements the fmt.Formatter interface
  93. so that it integrates cleanly with standard fmt package printing functions. The
  94. formatter is useful for inline printing of smaller data types similar to the
  95. standard %v format specifier.
  96. The custom formatter only responds to the %v (most compact), %+v (adds pointer
  97. addresses), %#v (adds types), or %#+v (adds types and pointer addresses) verb
  98. combinations. Any other verbs such as %x and %q will be sent to the the
  99. standard fmt package for formatting. In addition, the custom formatter ignores
  100. the width and precision arguments (however they will still work on the format
  101. specifiers not handled by the custom formatter).
  102. Custom Formatter Usage
  103. The simplest way to make use of the spew custom formatter is to call one of the
  104. convenience functions such as spew.Printf, spew.Println, or spew.Printf. The
  105. functions have syntax you are most likely already familiar with:
  106. spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2)
  107. spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
  108. spew.Println(myVar, myVar2)
  109. spew.Fprintf(os.Stderr, "myVar1: %v -- myVar2: %+v", myVar1, myVar2)
  110. spew.Fprintf(os.Stderr, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
  111. See the Index for the full list convenience functions.
  112. Sample Formatter Output
  113. Double pointer to a uint8:
  114. %v: <**>5
  115. %+v: <**>(0xf8400420d0->0xf8400420c8)5
  116. %#v: (**uint8)5
  117. %#+v: (**uint8)(0xf8400420d0->0xf8400420c8)5
  118. Pointer to circular struct with a uint8 field and a pointer to itself:
  119. %v: <*>{1 <*><shown>}
  120. %+v: <*>(0xf84003e260){ui8:1 c:<*>(0xf84003e260)<shown>}
  121. %#v: (*main.circular){ui8:(uint8)1 c:(*main.circular)<shown>}
  122. %#+v: (*main.circular)(0xf84003e260){ui8:(uint8)1 c:(*main.circular)(0xf84003e260)<shown>}
  123. See the Printf example for details on the setup of variables being shown
  124. here.
  125. Errors
  126. Since it is possible for custom Stringer/error interfaces to panic, spew
  127. detects them and handles them internally by printing the panic information
  128. inline with the output. Since spew is intended to provide deep pretty printing
  129. capabilities on structures, it intentionally does not return any errors.
  130. */
  131. package spew