2013-01-08 23:18:23 -06:00
go-spew
=======
2013-11-16 22:24:52 -06:00
[![Build Status ](https://travis-ci.org/davecgh/go-spew.png?branch=master )]
2014-03-27 05:20:09 -05:00
(https://travis-ci.org/davecgh/go-spew) [![Coverage Status]
2014-03-27 05:31:17 -05:00
(https://coveralls.io/repos/davecgh/go-spew/badge.png?branch=master)]
(https://coveralls.io/r/davecgh/go-spew?branch=master)
2013-11-16 21:44:48 -06:00
2013-01-08 23:51:09 -06:00
Go-spew implements a deep pretty printer for Go data structures to aid in
2013-01-20 22:39:13 -06:00
debugging. A comprehensive suite of tests with 100% test coverage is provided
to ensure proper functionality. See `test_coverage.txt` for the gocov coverage
report. Go-spew is licensed under the liberal ISC license, so it may be used in
open source or commercial projects.
2013-01-08 23:51:09 -06:00
2013-01-10 20:56:26 -06:00
If you're interested in reading about how this package came to life and some
2013-02-02 19:57:10 -06:00
of the challenges involved in providing a deep pretty printer, there is a blog
2013-01-10 20:56:26 -06:00
post about it
[here ](https://blog.cyphertite.com/go-spew-a-journey-into-dumping-go-data-structures/ ).
2013-01-08 23:51:09 -06:00
## Documentation
2013-11-16 16:10:27 -06:00
[![GoDoc ](https://godoc.org/github.com/davecgh/go-spew/spew?status.png )]
(http://godoc.org/github.com/davecgh/go-spew/spew)
2013-01-08 23:51:09 -06:00
Full `go doc` style documentation for the project can be viewed online without
installing this package by using the excellent GoDoc site here:
http://godoc.org/github.com/davecgh/go-spew/spew
You can also view the documentation locally once the package is installed with
the `godoc` tool by running `godoc -http=":6060"` and pointing your browser to
http://localhost:6060/pkg/github.com/davecgh/go-spew/spew
## Installation
```bash
2015-02-23 15:18:42 -06:00
$ go get -u github.com/davecgh/go-spew/spew
2013-01-08 23:51:09 -06:00
```
## Quick Start
2015-05-03 20:18:55 -05:00
Add this import line to the file you're working in:
```Go
2015-05-17 18:54:58 -05:00
import "github.com/davecgh/go-spew/spew"
2015-05-03 20:18:55 -05:00
```
2013-01-08 23:51:09 -06:00
To dump a variable with full newlines, indentation, type, and pointer
2013-03-11 18:40:54 -05:00
information use Dump, Fdump, or Sdump:
2013-01-08 23:51:09 -06:00
```Go
spew.Dump(myVar1, myVar2, ...)
spew.Fdump(someWriter, myVar1, myVar2, ...)
2013-03-11 18:40:54 -05:00
str := spew.Sdump(myVar1, myVar2, ...)
2013-01-08 23:51:09 -06:00
```
Alternatively, if you would prefer to use format strings with a compacted inline
2013-01-17 19:17:58 -06:00
printing style, use the convenience wrappers Printf, Fprintf, etc with %v (most
compact), %+v (adds pointer addresses), %#v (adds types), or %#+v (adds types
and pointer addresses):
2013-01-08 23:51:09 -06:00
```Go
spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2)
2013-01-17 19:17:58 -06:00
spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
2013-01-08 23:51:09 -06:00
spew.Fprintf(someWriter, "myVar1: %v -- myVar2: %+v", myVar1, myVar2)
2013-01-17 19:17:58 -06:00
spew.Fprintf(someWriter, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
2013-01-08 23:51:09 -06:00
```
2015-05-25 08:57:43 -05:00
## Debugging a Web Application Example
Here is an example of how you can use `spew.Sdump()` to help debug a web application. Please be sure to wrap your output using the `html.EscapeString()` function for safety reasons. You should also only use this debugging technique in a development environment, never in production.
```Go
package main
import (
"fmt"
"html"
"net/http"
"github.com/davecgh/go-spew/spew"
)
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, "Hi there, %s!", r.URL.Path[1:])
fmt.Fprintf(w, "<!-- \n" + html.EscapeString(spew.Sdump(w)) + " \n--> ")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
```
2013-01-08 23:51:09 -06:00
## Sample Dump Output
```
2013-01-08 23:55:23 -06:00
(main.Foo) {
unexportedField: (*main.Bar)(0xf84002e210)({
flag: (main.Flag) flagTwo,
data: (uintptr) < nil >
}),
ExportedField: (map[interface {}]interface {}) {
(string) "one": (bool) true
}
}
2013-03-08 22:45:02 -06:00
([]uint8) {
2013-03-08 22:29:03 -06:00
00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... |
00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%& '()*+,-./0|
00000020 31 32 |12|
}
2013-01-08 23:55:23 -06:00
```
## Sample Formatter Output
2013-01-17 19:17:58 -06:00
Double pointer to a uint8:
2013-01-08 23:55:23 -06:00
```
2013-01-17 19:17:58 -06:00
%v: < **>5
%+v: < **>(0xf8400420d0->0xf8400420c8)5
%#v: (**uint8)5
%#+v: (**uint8)(0xf8400420d0->0xf8400420c8)5
2013-01-08 23:55:23 -06:00
```
2013-01-17 19:17:58 -06:00
Pointer to circular struct with a uint8 field and a pointer to itself:
2013-01-08 23:55:23 -06:00
```
2013-01-17 19:17:58 -06:00
%v: < *>{1 < *>< shown > }
%+v: < *>(0xf84003e260){ui8:1 c:< *>(0xf84003e260)< shown > }
%#v: (*main.circular){ui8:(uint8)1 c:(*main.circular)< shown > }
%#+v: (*main.circular)(0xf84003e260){ui8:(uint8)1 c:(*main.circular)(0xf84003e260)< shown > }
2013-01-08 23:51:09 -06:00
```
2013-01-10 20:56:26 -06:00
## Configuration Options
2013-01-17 19:17:58 -06:00
Configuration of spew is handled by fields in the ConfigState type. For
convenience, all of the top-level functions use a global state available via the
spew.Config global.
2013-01-19 19:21:03 -06:00
It is also possible to create a ConfigState instance that provides methods
2013-01-17 19:17:58 -06:00
equivalent to the top-level functions. This allows concurrent configuration
2013-01-19 19:21:03 -06:00
options. See the ConfigState documentation for more details.
2013-01-17 19:17:58 -06:00
2013-01-10 20:56:26 -06:00
```
2013-01-17 19:17:58 -06:00
* Indent
2013-01-10 20:56:26 -06:00
String to use for each indentation level for Dump functions.
It is a single space by default. A popular alternative is "\t".
2013-01-19 19:21:03 -06:00
* MaxDepth
Maximum number of levels to descend into nested data structures.
There is no limit by default.
2013-01-17 19:17:58 -06:00
* DisableMethods
2013-01-10 20:56:26 -06:00
Disables invocation of error and Stringer interface methods.
Method invocation is enabled by default.
2013-01-17 19:17:58 -06:00
* DisablePointerMethods
2013-01-10 20:56:26 -06:00
Disables invocation of error and Stringer interface methods on types
which only accept pointer receivers from non-pointer variables.
Pointer method invocation is enabled by default.
2013-03-03 12:23:56 -06:00
* ContinueOnMethod
Enables recursion into types after invoking error and Stringer interface
methods. Recursion after method invocation is disabled by default.
2013-11-16 16:05:42 -06:00
* SortKeys
Specifies map keys should be sorted before being printed. Use
this to have a more deterministic, diffable output. Note that
only native types (bool, int, uint, floats, uintptr and string)
2015-03-31 15:55:04 -05:00
and types which implement error or Stringer interfaces are supported,
with other types sorted according to the reflect.Value.String() output
which guarantees display stability. Natural map order is used by
default.
* SpewKeys
SpewKeys specifies that, as a last resort attempt, map keys should be
spewed to strings and sorted by those strings. This is only considered
if SortKeys is true.
2013-01-10 20:56:26 -06:00
```
2013-01-08 23:51:09 -06:00
## License
Go-spew is licensed under the liberal ISC License.