当前位置:网站首页>调试利器 go-spew
调试利器 go-spew
2022-06-28 02:31:00 【15231181628】
对于应用的调试,我们经常会使用 fmt.Println来输出关键变量的数据。或者使用 log 库,将数据以 log 的形式输出。对于基础数据类型,上面两种方法都可以比较方便的满足需求。对于一些结构体类型数据通常我们可以先将其序列化后再输出。
如果结构体中包含不可序列化的字段,比如 func 类型,那么序列化就会抛出错误,阻碍调试。
go-spew
上面的需求,go-spew 可以完美的帮我们实现。go-spew 可以以一种非常友好的方式输出完整的数据结构信息。如:
s := "GoCN"
i := 123
spew.Dump(s, i)
//-----
(string) (len=4) "GoCN"
(int) 123
对于复杂的数据类型:
type S struct {
S2 *S2
I *int
}
type S2 struct {
Str string
}
func main() {
s := "GoCN"
i := 2
f := []float64{1.1, 2.2}
m := map[string]int{"a": 1, "b": 2}
e := errors.New("new error")
ss := S{S2: &S2{Str: "xxx"}, I: &i}
spew.Dump(s, i, f, m, e, ss)
}
//-----
(string) (len=4) "GoCN"
(int) 2
([]float64) (len=2 cap=2) {
(float64) 1.1,
(float64) 2.2
}
(map[string]int) (len=2) {
(string) (len=1) "a": (int) 1,
(string) (len=1) "b": (int) 2
}
(*errors.errorString)(0xc000010320)(new error)
(main.S) {
S2: (*main.S2)(0xc000010330)({
Str: (string) (len=3) "xxx"
}),
I: (*int)(0xc00001e1f0)(2)
}
可以看到,对于 map、slice、嵌套 struct 等类型的数据都可以友好的展示出来。包括长度、字段类型、字段值、指针值以及指针指向的数据等。
u := &url.URL{Scheme: "https", Host: "gocn.vip"}
req, err := http.NewRequestWithContext(context.Background(), "GET", u.String(), nil)
if err != nil {
panic(err)
}
spew.Dump(req)
//-----
(*http.Request)(0xc000162000)({
Method: (string) (len=3) "GET",
URL: (*url.URL)(0xc000136090)(https://gocn.vip),
Proto: (string) (len=8) "HTTP/1.1",
ProtoMajor: (int) 1,
ProtoMinor: (int) 1,
Header: (http.Header) {
},
Body: (io.ReadCloser) <nil>,
GetBody: (func() (io.ReadCloser, error)) <nil>,
ContentLength: (int64) 0,
TransferEncoding: ([]string) <nil>,
Close: (bool) false,
Host: (string) (len=8) "gocn.vip",
Form: (url.Values) <nil>,
PostForm: (url.Values) <nil>,
MultipartForm: (*multipart.Form)(<nil>),
Trailer: (http.Header) <nil>,
RemoteAddr: (string) "",
RequestURI: (string) "",
TLS: (*tls.ConnectionState)(<nil>),
Cancel: (<-chan struct {}) <nil>,
Response: (*http.Response)(<nil>),
ctx: (*context.emptyCtx)(0xc000020090)(context.Background)
})
上面是一个 http.Request类型的变量,其中包含多种复杂的数据类型,但 go-spew都可以友好的输出出来,非常方便我们调试。
Dump系列函数
go-spew有三个 Dump 系列函数:
Dump() 标准输出到os.Stdout Fdump() 允许输出自定义io.Writer Sdump() 输出的结果作为字符串返回
此外,还有 Printf、 Fprintf、Sprintf 几个函数来支持定制输出风格。用法与fmt的函数相似。
自定义配置
go-spew 支持一些自定义配置,可以通过spew.Config 修改。
一些常用配置如下:
Indent 修改分隔符 MaxDepth 控制遍历最大深度 DisablePointerAddresses 控制是否打印指针类型数据地址
此外还有其他很多配置,大家可以自己测试一下,这里不再举例。
小结
go-spew是一个非常完美的输出Go数据结构的调试工具,并且经过了全面的测试,测试覆盖率为100%。该工具支持各种自定义配置,非常方便,可以较大提升我们日常开发的效率。
References

边栏推荐
- Domain Name System
- nn.Parameter和torch.nn.init系列函数给模型参数初始化
- 爱普生L3153打印机如何清洗喷头
- 【活动早知道】LiveVideoStack近期活动一览
- 導入Excel文件,解决跳過空白單元格不讀取,並且下標前移的問題,以及RETURN_BLANK_AS_NULL報紅
- matlab习题 —— 数据的基本处理
- 剑指 Offer 47. 礼物的最大价值(DP)
- What are the technologies to be mastered in the test? Database design for software testing
- 【PaddleDetection】ModuleNotFoundError: No module named ‘paddle‘
- 将PCAP转换为Json文件的神器:joy(安装篇)
猜你喜欢
随机推荐
Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队
Tips for visiting the website: you are not authorized to view the recovery method of this page
Why is the service implementation class always red
启牛开的证券账户是安全的吗?如何开账户呢
Etcd database source code analysis -- network layer server rafthandler between clusters
RichView TRVStyle ParaStyles
空闲中断无法清除
多快好省,低门槛AI部署工具FastDeploy测试版来了!
Notepad++--常用的插件
How to write concise code? (upper)
matlab习题 —— 矩阵的常规运算
同样是MB,差距怎么这么大呢?
Heartless sword English Chinese bilingual poem 004 Meditation
爱普生L3153打印机如何清洗喷头
剑指 Offer 53 - I. 在排序数组中查找数字 I(改进二分)
__getitem__和__setitem__
Necessary software tools in embedded software development
Apache - Introduction à Apache
【522. 最长特殊序列 II】
Use js programming questions [split] in Niuke









