go-spew

为 Go 数据结构实现了一个非常漂亮的打印机,以帮助调试。(Implements a deep pretty printer for Go data structures to aid in debugging.)

Github stars Tracking Chart

go-spew

Go-spew 为 Go 数据结构实现了一个非常漂亮的打印机,以帮助调试。 提供了一整套测试,具有100%的测试覆盖率,以确保正确的功能。 有关 gocov 覆盖率报告,请参见test cover .txt。Go-spew 是在自由ISC许可下获得许可的,因此它可以用于开源或商业项目。

如果您有兴趣阅读这个软件包是如何实现的,以及提供深度漂亮的打印机所涉及的一些挑战,那么这里有一篇关于它的博客文章。

文档

项目的完整 godoc 风格的文档可以在线查看,而不需要安装这个包,可以使用这里优秀的 GoDoc 站点:http://godoc.org/github.com/davecgh/go-spew/spew

安装包之后,您还可以使用 godoc 工具、通过运行 godoc -http=":6060",并将浏览器指向 http://localhost:6060/pkg/github.com/davecgh/go-spew/spew,即可在本地查看文档。

安装

$ 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, ...)

或者,如果希望使用紧凑的内联打印样式的格式字符串,可以使用方便的包装器 Printf、Fprintf 等,其中包含 %v(最紧凑)、%+v(添加指针地址)、%#v(添加类型)或 %#+v(添加类型和指针地址):

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)

调试 Web 应用程序示例

下面是一个如何使用 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)
}

Damp 输出样例

(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|
}

Formatter 输出样例

指向 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 global 提供的全局状态。

也可以创建一个 ConfigState 实例,该实例提供与顶级函数等效的方法。 这允许并发配置选项。 有关更多详细信息,请参阅 ConfigState 文档。

* Indent
    用于转储函数的每个缩进级别的字符串。
    默认情况下它是一个空格。一个流行的选择是“\t”。
* MaxDepth
    下降到嵌套数据结构的最大级别数。
    默认情况下没有限制。
* DisableMethods
    禁用错误调用和Stringer接口方法。
    默认情况下启用方法调用。
* DisablePointerMethods
    禁用对仅接受来自非指针变量的指针接收器
    的类型的错误和 Stringer 接口方法的调用。
    此选项依赖于对不安全程序包的访问,因此
    在无法访问不安全程序包(如Google App Engine)
    或指定了“safe”构建标记的环境中运行时,它不会产生任何影响。
    默认情况下启用指针方法调用。
* DisablePointerAddresses
    DisablePointerAddresses指定是否禁用打印
    指针地址。在测试中区分数据结构时,这很有用。
* DisableCapacities
    DisableCapacities 指定是否禁用打印
    arrays, slices, maps 和 channels。
    在测试中区分数据结构时,这很有用。
* ContinueOnMethod
    在调用错误和 Stringer 接口后启用递归到类型
    方法。默认情况下禁用方法调用后的递归。
* SortKeys
    指定映射键应在打印前进行排序。使用它
    可以获得更确定的、可扩散的输出。请注意,
    仅支持原生类型(bool,int,uint,float,uintptr 和 string)
    以及实现错误或 Stringer 接口的类型,其他类型根据
     reflect.Value.String()输出进行排序,以确保显示稳定性。
    默认情况下使用自然映射顺序。
* SpewKeys
    SpewKeys 指出,作为最后的手段,map 键应该是
    注入字符串并按这些字符串排序。这只是考虑
    如果 SortKeys 为 true。

不安全的包依赖

此程序包依赖于不安全的程序包来执行某些更高级的功能,但它还支持“受限”模式,该模式允许它在不安全程序包不可用的环境中工作。 默认情况下,它将在 Google App Engine 上以此模式运行,并在使用 GopherJS 进行编译时运行。 还可以指定“安全”构建标记以强制包构建而不使用不安全的包。

许可

Go-spew 根据 copyfree ISC 许可证授权。

(First edition: vz edited at 2019.08.30)

Main metrics

Overview
Name With Ownerdavecgh/go-spew
Primary LanguageGo
Program languageShell (Language Count: 2)
PlatformLinux, Mac, Windows
License:ISC License
所有者活动
Created At2013-01-09 05:18:22
Pushed At2024-04-06 10:36:47
Last Commit At2018-08-30 15:11:22
Release Count3
Last Release Namev1.1.1 (Posted on 2018-08-17 03:41:05)
First Release Namev1.0.0 (Posted on 2016-08-16 12:45:36)
用户参与
Stargazers Count6.2k
Watchers Count69
Fork Count373
Commits Count145
Has Issues Enabled
Issues Count83
Issue Open Count43
Pull Requests Count28
Pull Requests Open Count23
Pull Requests Close Count15
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

go-spew

Build Status
ISC License
Coverage Status

Go-spew implements a deep pretty printer for Go data structures to aid in
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.

If you're interested in reading about how this package came to life and some
of the challenges involved in providing a deep pretty printer, there is a blog
post about it
here.

Documentation

GoDoc

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

$ go get -u github.com/davecgh/go-spew/spew

Quick Start

Add this import line to the file you're working in:

import "github.com/davecgh/go-spew/spew"

To dump a variable with full newlines, indentation, type, and pointer
information use Dump, Fdump, or Sdump:

spew.Dump(myVar1, myVar2, ...)
spew.Fdump(someWriter, myVar1, myVar2, ...)
str := spew.Sdump(myVar1, myVar2, ...)

Alternatively, if you would prefer to use format strings with a compacted inline
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):

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)

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.

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)
}

Sample Dump Output

(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, }

Sample Formatter Output

Double pointer to a uint8:

	  %v: <**>5
	 %+v: <**>(0xf8400420d0->0xf8400420c8)5
	 %#v: (**uint8)5
	%#+v: (**uint8)(0xf8400420d0->0xf8400420c8)5

Pointer to circular struct with a uint8 field and a pointer to itself:

	  %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>}

Configuration Options

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.

It is also possible to create a ConfigState instance that provides methods
equivalent to the top-level functions. This allows concurrent configuration
options. See the ConfigState documentation for more details.

* 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.

Unsafe Package Dependency

This package relies on the unsafe package to perform some of the more advanced
features, however it also supports a "limited" mode which allows it to work in
environments where the unsafe package is not available. By default, it will
operate in this mode on Google App Engine and when compiled with GopherJS. The
"safe" build tag may also be specified to force the package to build without
using the unsafe package.

License

Go-spew is licensed under the copyfree ISC License.