doc.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 error/Stringer interfaces are optionally invoked, including
  24. on unexported types
  25. * Custom types which only implement the error/Stringer 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 and %+v to provide inline printing similar
  34. to the default %v while providing the additional functionality outlined
  35. above and passing unsupported format verb/flag combinations such a %x,
  36. %q, and %#v 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 either
  46. %v (most compact) or %+v (adds pointer addresses):
  47. spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2)
  48. spew.Fprintf(someWriter, "myVar1: %v -- myVar2: %+v", myVar1, myVar2)
  49. Configuration Options
  50. The following configuration options are available:
  51. spew.Config.MaxDepth
  52. Maximum number of levels to descend into nested data structures.
  53. There is no limit by default.
  54. spew.Config.Indent
  55. String to use for each indentation level for Dump functions.
  56. It is a single space by default. A popular alternative is "\t".
  57. spew.Config.DisableMethods
  58. Disables invocation of error and Stringer interface methods.
  59. Method invocation is enabled by default.
  60. spew.Config.DisablePointerMethods
  61. Disables invocation of error and Stringer interface methods on types
  62. which only accept pointer receivers from non-pointer variables.
  63. Pointer method invocation is enabled by default.
  64. Dump Usage
  65. Simply call spew.Dump with a list of variables you want to dump:
  66. spew.Dump(myVar1, myVar2, ...)
  67. You may also call spew.Fdump if you would prefer to output to an arbitrary
  68. io.Writer. For example, to dump to standard error:
  69. spew.Fdump(os.Stderr, myVar1, myVar2, ...)
  70. Sample Dump Output
  71. See the Dump example for details on the setup of the types and variables being
  72. shown here.
  73. (main.Foo) {
  74. unexportedField: (*main.Bar)(0xf84002e210)({
  75. flag: (main.Flag) flagTwo,
  76. data: (uintptr) <nil>
  77. }),
  78. ExportedField: (map[interface {}]interface {}) {
  79. (string) "one": (bool) true
  80. }
  81. }
  82. Custom Formatter
  83. Spew provides a custom formatter the implements the fmt.Formatter interface
  84. so that it integrates cleanly with standard fmt package printing functions. The
  85. formatter is useful for inline printing of smaller data types similar to the
  86. standard %v format specifier.
  87. The spew formatter only responds to the %v and %+v verb combinations. Any other
  88. variations such as %x, %q, and %#v will be sent to the the standard fmt package
  89. for formatting. In addition, the spew formatter ignores the width and precision
  90. arguments (however they will still work on the format specifiers spew does not
  91. handle).
  92. Custom Formatter Usage
  93. The simplest way to make use of the spew custom formatter is to call one of the
  94. convenience functions such as spew.Printf, spew.Println, or spew.Printf. The
  95. functions have the exact same syntax you are most likely already familiar with:
  96. spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2)
  97. spew.Println(myVar, myVar2)
  98. spew.Fprintf(os.Stderr, "myVar1: %v -- myVar2: %+v", myVar1, myVar2)
  99. See the Index for the full list convenience functions.
  100. Sample Formatter Output
  101. Double pointer to a uint8 via %v:
  102. <**>5
  103. Circular struct with a uint8 field and a pointer to itself via %+v:
  104. {ui8:1 c:<*>(0xf84002d200){ui8:1 c:<*>(0xf84002d200)<shown>}}
  105. See the Printf example for details on the setup of variables being shown
  106. here.
  107. Errors
  108. Since it is possible for custom error/Stringer interfaces to panic, spew
  109. detects them and handles them internally by printing the panic information
  110. inline with the output. Since spew is intended to provide deep pretty printing
  111. capabilities on structures, it intentionally does not return any errors.
  112. */
  113. package spew