当前位置:网站首页>调试利器 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

边栏推荐
- [postgraduate] bit by bit
- matlab习题 —— 矩阵的常规运算
- 启牛开的证券账户是安全的吗?如何开账户呢
- RichView TRVStyle ParaStyles
- idea自动生成代码
- Redis搭建集群【简单】
- 根据Explain查看sql执行计划,对SQL进行优化
- Import an excel file, solve the problem of skipping blank cells without reading and moving the subscript forward, and return_ BLANK_ AS_ Null red
- Question bank and answers of special operation certificate for R1 quick opening pressure vessel operation in 2022
- Simple file transfer protocol TFTP
猜你喜欢
2022年R1快開門式壓力容器操作特種作業證考試題庫及答案
ETCD数据库源码分析——集群间网络层服务端RaftHandler
Thesis reading: General advantageous transformers
腾讯游戏发布40多款产品与项目 其中12款为新游戏
导入Excel文件,解决跳过空白单元格不读取,并且下标前移的问题,以及RETURN_BLANK_AS_NULL报红
【PaddleDetection】ModuleNotFoundError: No module named ‘paddle‘
Gateway microservice routing failed to load microservice static resources
您的物联网安全性是否足够强大?
建立自己的网站(17)
collections.defaultdict()的使用
随机推荐
启牛开的证券账户是安全的吗?如何开账户呢
嵌入式DSP音频开发
AgilePLM异常解决-Session篇
Artifact for converting pcap to JSON file: joy (installation)
matlab习题 —— 矩阵的常规运算
apache、iis6、ii7独立ip主机屏蔽限制ip访问
云成本优化有哪些优秀实践?
华为设备WLAN基本业务配置命令
第二轮红队免费公开课来袭~明晚八点!
Hot! Yolov6's fast and accurate target detection framework is open source (with source code download)
2022电工(初级)复训题库及在线模拟考试
简单ELK配置实现生产级别的日志采集和查询实践
Apache——阿帕奇简介
2022年R1快開門式壓力容器操作特種作業證考試題庫及答案
Is it better for a novice to open a securities account? Is it safe to open a stock trading account
多快好省,低门槛AI部署工具FastDeploy测试版来了!
Gateway微服務路由使微服務靜態資源加載失敗
Flow based depth generation model
Heartless sword Chinese English bilingual poem 004 Sword
js清空对象和对象的值: