Go-spew
Go-spew为Go数据结构实现了一个深度漂亮的打印机,以帮助调试。提供了一套全面的测试,测试覆盖率为100%,以确保功能正常。参见test_coverage.txt 获取 gocov 覆盖率报告。Go-spew是在自由的ISC许可证下授权的,所以它可以被用于开源或商业项目。
如果你有兴趣阅读关于这个包是如何诞生的,以及在提供深度漂亮的打印机方面的一些挑战,这里有一篇关于它的博文。
文档
该项目完整的go doc 风格的文档可以通过使用优秀的GoDoc网站在线查看:http://godoc.org/github.com/davecgh/go-spew/spew,无需安装该软件包。
你也可以通过运行godoc -http=":6060" ,将你的浏览器指向http://localhost:6060/pkg/github.com/davecgh/go-spew/spew,在本地通过godoc 工具安装该软件包后,查看该文档。
安装
$ go get -u github.com/davecgh/go-spew/spew
快速启动
在你工作的文件中添加这个导入行:
import "github.com/davecgh/go-spew/spew"
使用Dump、Fdump或Sdump来转储一个带有完整换行、缩进、类型和指针信息的变量:
spew.Dump(myVar1, myVar2, ...)
spew.Fdump(someWriter, myVar1, myVar2, ...)
str := spew.Sdump(myVar1, myVar2, ...)
另外,如果你喜欢使用具有紧凑的内联打印风格的格式字符串,可以使用带有%v(最紧凑)、%+v(增加指针地址)、%#v(增加类型)或%#+v(增加类型和指针地址)的方便包装器Printf、Fprintf等等。
spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2)
spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
spew.Fprintf(someWriter, "myVar1: %v -- myVar2: %+v", myVar1, myVar2)
spew.Fprintf(someWriter, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
调试网络应用实例
下面是一个如何使用spew.Sdump() 来帮助调试Web应用程序的例子。为了安全起见,请确保使用html.EscapeString() 函数来包装你的输出。你也应该只在开发环境中使用这种调试技术,而不是在生产环境中。
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)
}
倾倒输出示例
(main.Foo) {
unexportedField: (*main.Bar)(0xf84002e210)({
flag: (main.Flag) flagTwo,
data: (uintptr) <nil>
}),
ExportedField: (map[interface {}]interface {}) {
(string) "one": (bool) true
}
}
([]uint8) {
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|
}
格式化输出示例
指向一个uint8的双倍指针:
%v: <**>5
%+v: <**>(0xf8400420d0->0xf8400420c8)5
%#v: (**uint8)5
%#+v: (**uint8)(0xf8400420d0->0xf8400420c8)5
指向带有一个uint8字段和一个指向自身的指针的圆形结构的指针。
%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>}
配置选项
spew的配置是由ConfigState类型中的字段处理的。为了方便起见,所有的顶层函数都使用通过spew.Config全局提供的全局状态。
也可以创建一个ConfigState实例,它提供了与顶级函数相当的方法。这允许并发的配置选项。更多细节见ConfigState文档:
* Indent
String to use for each indentation level for Dump functions.
It is a single space by default. A popular alternative is "\t".
* MaxDepth
Maximum number of levels to descend into nested data structures.
There is no limit by default.
* DisableMethods
Disables invocation of error and Stringer interface methods.
Method invocation is enabled by default.
* DisablePointerMethods
Disables invocation of error and Stringer interface methods on types
which only accept pointer receivers from non-pointer variables. This option
relies on access to the unsafe package, so it will not have any effect when
running in environments without access to the unsafe package such as Google
App Engine or with the "safe" build tag specified.
Pointer method invocation is enabled by default.
* DisablePointerAddresses
DisablePointerAddresses specifies whether to disable the printing of
pointer addresses. This is useful when diffing data structures in tests.
* DisableCapacities
DisableCapacities specifies whether to disable the printing of capacities
for arrays, slices, maps and channels. This is useful when diffing data
structures in tests.
* ContinueOnMethod
Enables recursion into types after invoking error and Stringer interface
methods. Recursion after method invocation is disabled by default.
* 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)
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.
不安全包的依赖性
这个包依赖不安全包来执行一些更高级的功能,但是它也支持 "有限 "模式,允许它在不安全包不可用的环境下工作。默认情况下,它将在谷歌应用引擎上以这种模式运行,并且在用GopherJS编译时也是如此。也可以指定 "安全 "构建标签来强制包在不使用不安全包的情况下构建。