转载

将go 对象输出成为tree

无论是php的var_dump,还是js的consolg.log函数,都可以很方便的查看对象,方便调试.但是golang不是那么的方便,把输出打印到一行,而且指针仅仅是显示他的内存位置,不解引用输出对象.

调试不方便,怎么办,自己写一个来处理一下吧. 开源地址: https://github.com/Maxgis/tree

举个例子:

旧方案-原始输出

&tree.People{Name:"jim", Age:44, sex:"man", Father:(*tree.People)(nil), Children:[]*tree.People{(*tree.People)(0xc820014280), (*tree.People)(0xc8200142d0)}, Pro:map[string]string{"city":"Beijing"}}

新方案-格式化输出

{   Name:string("jim"),   Age:int(44),   sex:string("man"),   Father:*tree.People(nil),   Children:[     {       Name:string("jimson"),       Age:int(12),       sex:string("boy"),       Father:*tree.People(c820014230,reference to each other),       Children:[],       Pro:{}(map[string]string)     }(*tree.People),     {       Name:string("jimdaughter"),       Age:int(14),       sex:string("girl"),       Father:*tree.People(nil),       Children:[],       Pro:{}(map[string]string)     }(*tree.People)   ],   Pro:{     city:string("Beijing")   }(map[string]string) }(*tree.People)

开源地址: https://github.com/Maxgis/tree

原文  http://www.woniubi.cn/go_object_print_as_tree/
正文到此结束
Loading...