github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/test/go-spew/README.md (about) 1 go-spew 2 ======= 3 4 [![Build Status](https://img.shields.io/travis/davecgh/go-spew.svg)] 5 (https://travis-ci.org/davecgh/go-spew) [![ISC License] 6 (http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org) [![Coverage Status] 7 (https://img.shields.io/coveralls/davecgh/go-spew.svg)] 8 (https://coveralls.io/r/davecgh/go-spew?branch=master) 9 10 11 Go-spew implements a deep pretty printer for Go data structures to aid in 12 debugging. A comprehensive suite of tests with 100% test coverage is provided 13 to ensure proper functionality. See `test_coverage.txt` for the gocov coverage 14 report. Go-spew is licensed under the liberal ISC license, so it may be used in 15 open source or commercial projects. 16 17 If you're interested in reading about how this package came to life and some 18 of the challenges involved in providing a deep pretty printer, there is a blog 19 post about it 20 [here](https://web.archive.org/web/20160304013555/https://blog.cyphertite.com/go-spew-a-journey-into-dumping-go-data-structures/). 21 22 ## Documentation 23 24 [![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)] 25 (http://godoc.org/github.com/davecgh/go-spew/spew) 26 27 Full `go doc` style documentation for the project can be viewed online without 28 installing this package by using the excellent GoDoc site here: 29 http://godoc.org/github.com/davecgh/go-spew/spew 30 31 You can also view the documentation locally once the package is installed with 32 the `godoc` tool by running `godoc -http=":6060"` and pointing your browser to 33 http://localhost:6060/pkg/github.com/davecgh/go-spew/spew 34 35 ## Installation 36 37 ```bash 38 $ go get -u github.com/davecgh/go-spew/spew 39 ``` 40 41 ## Quick Start 42 43 Add this import line to the file you're working in: 44 45 ```Go 46 import "github.com/davecgh/go-spew/spew" 47 ``` 48 49 To dump a variable with full newlines, indentation, type, and pointer 50 information use Dump, Fdump, or Sdump: 51 52 ```Go 53 spew.Dump(myVar1, myVar2, ...) 54 spew.Fdump(someWriter, myVar1, myVar2, ...) 55 str := spew.Sdump(myVar1, myVar2, ...) 56 ``` 57 58 Alternatively, if you would prefer to use format strings with a compacted inline 59 printing style, use the convenience wrappers Printf, Fprintf, etc with %v (most 60 compact), %+v (adds pointer addresses), %#v (adds types), or %#+v (adds types 61 and pointer addresses): 62 63 ```Go 64 spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2) 65 spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 66 spew.Fprintf(someWriter, "myVar1: %v -- myVar2: %+v", myVar1, myVar2) 67 spew.Fprintf(someWriter, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 68 ``` 69 70 ## Debugging a Web Application Example 71 72 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. 73 74 ```Go 75 package main 76 77 import ( 78 "fmt" 79 "html" 80 "net/http" 81 82 "github.com/davecgh/go-spew/spew" 83 ) 84 85 func handler(w http.ResponseWriter, r *http.Request) { 86 w.Header().Set("Content-Type", "text/html") 87 fmt.Fprintf(w, "Hi there, %s!", r.URL.Path[1:]) 88 fmt.Fprintf(w, "<!--\n" + html.EscapeString(spew.Sdump(w)) + "\n-->") 89 } 90 91 func main() { 92 http.HandleFunc("/", handler) 93 http.ListenAndServe(":8080", nil) 94 } 95 ``` 96 97 ## Sample Dump Output 98 99 ``` 100 (main.Foo) { 101 unexportedField: (*main.Bar)(0xf84002e210)({ 102 flag: (main.Flag) flagTwo, 103 data: (uintptr) <nil> 104 }), 105 ExportedField: (map[interface {}]interface {}) { 106 (string) "one": (bool) true 107 } 108 } 109 ([]uint8) { 110 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... | 111 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0| 112 00000020 31 32 |12| 113 } 114 ``` 115 116 ## Sample Formatter Output 117 118 Double pointer to a uint8: 119 ``` 120 %v: <**>5 121 %+v: <**>(0xf8400420d0->0xf8400420c8)5 122 %#v: (**uint8)5 123 %#+v: (**uint8)(0xf8400420d0->0xf8400420c8)5 124 ``` 125 126 Pointer to circular struct with a uint8 field and a pointer to itself: 127 ``` 128 %v: <*>{1 <*><shown>} 129 %+v: <*>(0xf84003e260){ui8:1 c:<*>(0xf84003e260)<shown>} 130 %#v: (*main.circular){ui8:(uint8)1 c:(*main.circular)<shown>} 131 %#+v: (*main.circular)(0xf84003e260){ui8:(uint8)1 c:(*main.circular)(0xf84003e260)<shown>} 132 ``` 133 134 ## Configuration Options 135 136 Configuration of spew is handled by fields in the ConfigState type. For 137 convenience, all of the top-level functions use a global state available via the 138 spew.Config global. 139 140 It is also possible to create a ConfigState instance that provides methods 141 equivalent to the top-level functions. This allows concurrent configuration 142 options. See the ConfigState documentation for more details. 143 144 ``` 145 * Indent 146 String to use for each indentation level for Dump functions. 147 It is a single space by default. A popular alternative is "\t". 148 149 * MaxDepth 150 Maximum number of levels to descend into nested data structures. 151 There is no limit by default. 152 153 * DisableMethods 154 Disables invocation of error and Stringer interface methods. 155 Method invocation is enabled by default. 156 157 * DisablePointerMethods 158 Disables invocation of error and Stringer interface methods on types 159 which only accept pointer receivers from non-pointer variables. This option 160 relies on access to the unsafe package, so it will not have any effect when 161 running in environments without access to the unsafe package such as Google 162 App Engine or with the "safe" build tag specified. 163 Pointer method invocation is enabled by default. 164 165 * DisablePointerAddresses 166 DisablePointerAddresses specifies whether to disable the printing of 167 pointer addresses. This is useful when diffing data structures in tests. 168 169 * DisableCapacities 170 DisableCapacities specifies whether to disable the printing of capacities 171 for arrays, slices, maps and channels. This is useful when diffing data 172 structures in tests. 173 174 * ContinueOnMethod 175 Enables recursion into types after invoking error and Stringer interface 176 methods. Recursion after method invocation is disabled by default. 177 178 * SortKeys 179 Specifies map keys should be sorted before being printed. Use 180 this to have a more deterministic, diffable output. Note that 181 only native types (bool, int, uint, floats, uintptr and string) 182 and types which implement error or Stringer interfaces are supported, 183 with other types sorted according to the reflect.Value.String() output 184 which guarantees display stability. Natural map order is used by 185 default. 186 187 * SpewKeys 188 SpewKeys specifies that, as a last resort attempt, map keys should be 189 spewed to strings and sorted by those strings. This is only considered 190 if SortKeys is true. 191 192 ``` 193 194 ## Unsafe Package Dependency 195 196 This package relies on the unsafe package to perform some of the more advanced 197 features, however it also supports a "limited" mode which allows it to work in 198 environments where the unsafe package is not available. By default, it will 199 operate in this mode on Google App Engine and when compiled with GopherJS. The 200 "safe" build tag may also be specified to force the package to build without 201 using the unsafe package. 202 203 ## License 204 205 Go-spew is licensed under the [copyfree](http://copyfree.org) ISC License.