kr / pretty Goto Github PK
View Code? Open in Web Editor NEWPretty printing for Go values
Home Page: http://godoc.org/github.com/kr/pretty
License: MIT License
Pretty printing for Go values
Home Page: http://godoc.org/github.com/kr/pretty
License: MIT License
Can you please add a license such as Apache 2, BSD, or MIT. Without a license we don't know the terms of the code for use in our own applications and libraries.
Until there is a license I can't use this package.
Hi there,
I wanted to know if there is a way how I can skip zero values of struct? I couldn't find anything in the documentation.
Thanks,
Sebastian
I just printed a time field and got this out. I understand that might be the desired behaviour in some cases, but it would be nice if there was a way to specify that a given type should have a simplified pretty form - this is a bit silly.
mytimefield: {
sec: 63547322631,
nsec: 0x1ec3a8c8,
loc: &time.Location{
name: "Local",
zone: {
{name:"BST", offset:3600, isDST:true},
{name:"GMT", offset:0, isDST:false},
{name:"BDST", offset:7200, isDST:true},
{name:"BST", offset:3600, isDST:false},
{name:"BST", offset:3600, isDST:true},
{name:"GMT", offset:0, isDST:false},
{name:"GMT", offset:0, isDST:false},
},
tx: {
{when:-1691964000, index:0x0, isstd:true, isutc:false},
{when:-1680472800, index:0x1, isstd:true, isutc:false},
{when:-1664143200, index:0x0, isstd:true, isutc:false},
{when:-1650146400, index:0x1, isstd:false, isutc:false},
{when:-1633903200, index:0x0, isstd:true, isutc:true},
{when:-1617487200, index:0x1, isstd:true, isutc:true},
{when:-1601848800, index:0x0, isstd:false, isutc:false},
{when:-1586037600, index:0x1, isstd:false, isutc:false},
{when:-1570399200, index:0x0, isstd:false, isutc:false},
{when:-1552168800, index:0x1, isstd:false, isutc:false},
{when:-1538344800, index:0x0, isstd:false, isutc:false},
{when:-1522533600, index:0x1, isstd:false, isutc:false},
{when:-1507500000, index:0x0, isstd:false, isutc:false},
{when:-1490565600, index:0x1, isstd:false, isutc:false},
{when:-1473631200, index:0x0, isstd:false, isutc:false},
{when:-1460930400, index:0x1, isstd:false, isutc:false},
{when:-1442786400, index:0x0, isstd:false, isutc:false},
{when:-1428876000, index:0x1, isstd:false, isutc:false},
{when:-1410732000, index:0x0, isstd:false, isutc:false},
{when:-1396216800, index:0x1, isstd:false, isutc:false},
{when:-1379282400, index:0x0, isstd:false, isutc:false},
{when:-1364767200, index:0x1, isstd:false, isutc:false},
{when:-1348437600, index:0x0, isstd:false, isutc:false},
{when:-1333317600, index:0x1, isstd:false, isutc:false},
{when:-1315778400, index:0x0, isstd:false, isutc:false},
{when:-1301263200, index:0x1, isstd:false, isutc:false},
{when:-1284328800, index:0x0, isstd:false, isutc:false},
{when:-1269813600, index:0x1, isstd:false, isutc:false},
{when:-1253484000, index:0x0, isstd:false, isutc:false},
{when:-1238364000, index:0x1, isstd:false, isutc:false},
{when:-1221429600, index:0x0, isstd:false, isutc:false},
{when:-1206914400, index:0x1, isstd:false, isutc:false},
{when:-1189980000, index:0x0, isstd:false, isutc:false},
{when:-1175464800, index:0x1, isstd:false, isutc:false},
{when:-1159135200, index:0x0, isstd:false, isutc:false},
{when:-1143410400, index:0x1, isstd:false, isutc:false},
{when:-1126476000, index:0x0, isstd:false, isutc:false},
{when:-1111960800, index:0x1, isstd:false, isutc:false},
{when:-1095631200, index:0x0, isstd:false, isutc:false},
{when:-1080511200, index:0x1, isstd:false, isutc:false},
{when:-1063576800, index:0x0, isstd:false, isutc:false},
{when:-1049061600, index:0x1, isstd:false, isutc:false},
{when:-1032127200, index:0x0, isstd:false, isutc:false},
{when:-1017612000, index:0x1, isstd:false, isutc:false},
{when:-1001282400, index:0x0, isstd:false, isutc:false},
{when:-986162400, index:0x1, isstd:false, isutc:false},
{when:-969228000, index:0x0, isstd:false, isutc:false},
{when:-950479200, index:0x1, isstd:false, isutc:false},
{when:-942012000, index:0x0, isstd:false, isutc:false},
{when:-904518000, index:0x2, isstd:false, isutc:false},
{when:-896050800, index:0x0, isstd:false, isutc:false},
{when:-875487600, index:0x2, isstd:false, isutc:false},
{when:-864601200, index:0x0, isstd:false, isutc:false},
{when:-844038000, index:0x2, isstd:false, isutc:false},
{when:-832546800, index:0x0, isstd:false, isutc:false},
{when:-812588400, index:0x2, isstd:false, isutc:false},
{when:-798073200, index:0x0, isstd:false, isutc:false},
{when:-781052400, index:0x2, isstd:false, isutc:false},
{when:-772066800, index:0x0, isstd:false, isutc:false},
{when:-764805600, index:0x1, isstd:false, isutc:false},
{when:-748476000, index:0x0, isstd:false, isutc:false},
{when:-733356000, index:0x1, isstd:false, isutc:false},
{when:-719445600, index:0x0, isstd:false, isutc:false},
{when:-717030000, index:0x2, isstd:false, isutc:false},
{when:-706748400, index:0x0, isstd:false, isutc:false},
{when:-699487200, index:0x1, isstd:false, isutc:false},
{when:-687996000, index:0x0, isstd:false, isutc:false},
{when:-668037600, index:0x1, isstd:false, isutc:false},
{when:-654732000, index:0x0, isstd:false, isutc:false},
{when:-636588000, index:0x1, isstd:false, isutc:false},
{when:-622072800, index:0x0, isstd:false, isutc:false},
{when:-605743200, index:0x1, isstd:false, isutc:false},
{when:-590623200, index:0x0, isstd:false, isutc:false},
{when:-574293600, index:0x1, isstd:false, isutc:false},
{when:-558568800, index:0x0, isstd:false, isutc:false},
{when:-542239200, index:0x1, isstd:false, isutc:false},
{when:-527119200, index:0x0, isstd:false, isutc:false},
{when:-512604000, index:0x1, isstd:false, isutc:false},
{when:-496274400, index:0x0, isstd:false, isutc:false},
{when:-481154400, index:0x1, isstd:false, isutc:false},
{when:-464220000, index:0x0, isstd:false, isutc:false},
{when:-449704800, index:0x1, isstd:false, isutc:false},
{when:-432165600, index:0x0, isstd:false, isutc:false},
{when:-417650400, index:0x1, isstd:false, isutc:false},
{when:-401320800, index:0x0, isstd:false, isutc:false},
{when:-386200800, index:0x1, isstd:false, isutc:false},
{when:-369266400, index:0x0, isstd:false, isutc:false},
{when:-354751200, index:0x1, isstd:false, isutc:false},
{when:-337816800, index:0x0, isstd:false, isutc:false},
{when:-323301600, index:0x1, isstd:false, isutc:false},
{when:-306972000, index:0x0, isstd:false, isutc:false},
{when:-291852000, index:0x1, isstd:false, isutc:false},
{when:-276732000, index:0x0, isstd:false, isutc:false},
{when:-257983200, index:0x1, isstd:false, isutc:false},
{when:-245282400, index:0x0, isstd:false, isutc:false},
{when:-226533600, index:0x1, isstd:false, isutc:false},
{when:-213228000, index:0x0, isstd:false, isutc:false},
{when:-195084000, index:0x1, isstd:false, isutc:false},
{when:-182383200, index:0x0, isstd:false, isutc:false},
{when:-163634400, index:0x1, isstd:false, isutc:false},
{when:-150933600, index:0x0, isstd:false, isutc:false},
{when:-132184800, index:0x1, isstd:false, isutc:false},
{when:-119484000, index:0x0, isstd:false, isutc:false},
{when:-100735200, index:0x1, isstd:false, isutc:false},
{when:-88034400, index:0x0, isstd:false, isutc:false},
{when:-68680800, index:0x1, isstd:false, isutc:false},
{when:-59004000, index:0x0, isstd:false, isutc:false},
{when:-37242000, index:0x3, isstd:false, isutc:false},
{when:57722400, index:0x5, isstd:false, isutc:false},
{when:69818400, index:0x0, isstd:false, isutc:false},
{when:89172000, index:0x1, isstd:false, isutc:false},
{when:101268000, index:0x0, isstd:false, isutc:false},
{when:120621600, index:0x1, isstd:false, isutc:false},
{when:132717600, index:0x0, isstd:false, isutc:false},
{when:152071200, index:0x1, isstd:false, isutc:false},
{when:164167200, index:0x0, isstd:false, isutc:false},
{when:183520800, index:0x1, isstd:false, isutc:false},
{when:196221600, index:0x0, isstd:false, isutc:false},
{when:214970400, index:0x1, isstd:false, isutc:false},
{when:227671200, index:0x0, isstd:false, isutc:false},
{when:246420000, index:0x1, isstd:false, isutc:false},
{when:259120800, index:0x0, isstd:false, isutc:false},
{when:278474400, index:0x1, isstd:false, isutc:false},
{when:290570400, index:0x0, isstd:false, isutc:false},
{when:309924000, index:0x1, isstd:false, isutc:false},
{when:322020000, index:0x0, isstd:false, isutc:false},
{when:341373600, index:0x1, isstd:false, isutc:false},
{when:354675600, index:0x4, isstd:false, isutc:false},
{when:372819600, index:0x5, isstd:false, isutc:false},
{when:386125200, index:0x4, isstd:false, isutc:false},
{when:404269200, index:0x5, isstd:false, isutc:false},
{when:417574800, index:0x4, isstd:false, isutc:false},
{when:435718800, index:0x5, isstd:false, isutc:false},
{when:449024400, index:0x4, isstd:false, isutc:false},
{when:467773200, index:0x5, isstd:false, isutc:false},
{when:481078800, index:0x4, isstd:false, isutc:false},
{when:499222800, index:0x5, isstd:false, isutc:false},
{when:512528400, index:0x4, isstd:false, isutc:false},
{when:530672400, index:0x5, isstd:false, isutc:false},
{when:543978000, index:0x4, isstd:false, isutc:false},
{when:562122000, index:0x5, isstd:false, isutc:false},
{when:575427600, index:0x4, isstd:false, isutc:false},
{when:593571600, index:0x5, isstd:false, isutc:false},
{when:606877200, index:0x4, isstd:false, isutc:false},
{when:625626000, index:0x5, isstd:false, isutc:false},
{when:638326800, index:0x4, isstd:false, isutc:false},
{when:657075600, index:0x5, isstd:false, isutc:false},
{when:670381200, index:0x4, isstd:false, isutc:false},
{when:688525200, index:0x5, isstd:false, isutc:false},
{when:701830800, index:0x4, isstd:false, isutc:false},
{when:719974800, index:0x5, isstd:false, isutc:false},
{when:733280400, index:0x4, isstd:false, isutc:false},
{when:751424400, index:0x5, isstd:false, isutc:false},
{when:764730000, index:0x4, isstd:false, isutc:false},
{when:782874000, index:0x5, isstd:false, isutc:false},
{when:796179600, index:0x4, isstd:false, isutc:false},
{when:814323600, index:0x5, isstd:false, isutc:false},
{when:820454400, index:0x6, isstd:false, isutc:false},
{when:828234000, index:0x4, isstd:false, isutc:false},
{when:846378000, index:0x5, isstd:false, isutc:false},
{when:859683600, index:0x4, isstd:false, isutc:false},
{when:877827600, index:0x5, isstd:false, isutc:false},
{when:891133200, index:0x4, isstd:false, isutc:false},
{when:909277200, index:0x5, isstd:false, isutc:false},
{when:922582800, index:0x4, isstd:false, isutc:false},
{when:941331600, index:0x5, isstd:false, isutc:false},
{when:954032400, index:0x4, isstd:false, isutc:false},
{when:972781200, index:0x5, isstd:false, isutc:false},
{when:985482000, index:0x4, isstd:false, isutc:false},
{when:1004230800, index:0x5, isstd:false, isutc:false},
{when:1017536400, index:0x4, isstd:false, isutc:false},
{when:1035680400, index:0x5, isstd:false, isutc:false},
{when:1048986000, index:0x4, isstd:false, isutc:false},
{when:1067130000, index:0x5, isstd:false, isutc:false},
{when:1080435600, index:0x4, isstd:false, isutc:false},
{when:1099184400, index:0x5, isstd:false, isutc:false},
{when:1111885200, index:0x4, isstd:false, isutc:false},
{when:1130634000, index:0x5, isstd:false, isutc:false},
{when:1143334800, index:0x4, isstd:false, isutc:false},
{when:1162083600, index:0x5, isstd:false, isutc:false},
{when:1174784400, index:0x4, isstd:false, isutc:false},
{when:1193533200, index:0x5, isstd:false, isutc:false},
{when:1206838800, index:0x4, isstd:false, isutc:false},
{when:1224982800, index:0x5, isstd:false, isutc:false},
{when:1238288400, index:0x4, isstd:false, isutc:false},
{when:1256432400, index:0x5, isstd:false, isutc:false},
{when:1269738000, index:0x4, isstd:false, isutc:false},
{when:1288486800, index:0x5, isstd:false, isutc:false},
{when:1301187600, index:0x4, isstd:false, isutc:false},
{when:1319936400, index:0x5, isstd:false, isutc:false},
{when:1332637200, index:0x4, isstd:false, isutc:false},
{when:1351386000, index:0x5, isstd:false, isutc:false},
{when:1364691600, index:0x4, isstd:false, isutc:false},
{when:1382835600, index:0x5, isstd:false, isutc:false},
{when:1396141200, index:0x4, isstd:false, isutc:false},
{when:1414285200, index:0x5, isstd:false, isutc:false},
{when:1427590800, index:0x4, isstd:false, isutc:false},
{when:1445734800, index:0x5, isstd:false, isutc:false},
{when:1459040400, index:0x4, isstd:false, isutc:false},
{when:1477789200, index:0x5, isstd:false, isutc:false},
{when:1490490000, index:0x4, isstd:false, isutc:false},
{when:1509238800, index:0x5, isstd:false, isutc:false},
{when:1521939600, index:0x4, isstd:false, isutc:false},
{when:1540688400, index:0x5, isstd:false, isutc:false},
{when:1553994000, index:0x4, isstd:false, isutc:false},
{when:1572138000, index:0x5, isstd:false, isutc:false},
{when:1585443600, index:0x4, isstd:false, isutc:false},
{when:1603587600, index:0x5, isstd:false, isutc:false},
{when:1616893200, index:0x4, isstd:false, isutc:false},
{when:1635642000, index:0x5, isstd:false, isutc:false},
{when:1648342800, index:0x4, isstd:false, isutc:false},
{when:1667091600, index:0x5, isstd:false, isutc:false},
{when:1679792400, index:0x4, isstd:false, isutc:false},
{when:1698541200, index:0x5, isstd:false, isutc:false},
{when:1711846800, index:0x4, isstd:false, isutc:false},
{when:1729990800, index:0x5, isstd:false, isutc:false},
{when:1743296400, index:0x4, isstd:false, isutc:false},
{when:1761440400, index:0x5, isstd:false, isutc:false},
{when:1774746000, index:0x4, isstd:false, isutc:false},
{when:1792890000, index:0x5, isstd:false, isutc:false},
{when:1806195600, index:0x4, isstd:false, isutc:false},
{when:1824944400, index:0x5, isstd:false, isutc:false},
{when:1837645200, index:0x4, isstd:false, isutc:false},
{when:1856394000, index:0x5, isstd:false, isutc:false},
{when:1869094800, index:0x4, isstd:false, isutc:false},
{when:1887843600, index:0x5, isstd:false, isutc:false},
{when:1901149200, index:0x4, isstd:false, isutc:false},
{when:1919293200, index:0x5, isstd:false, isutc:false},
{when:1932598800, index:0x4, isstd:false, isutc:false},
{when:1950742800, index:0x5, isstd:false, isutc:false},
{when:1964048400, index:0x4, isstd:false, isutc:false},
{when:1982797200, index:0x5, isstd:false, isutc:false},
{when:1995498000, index:0x4, isstd:false, isutc:false},
{when:2014246800, index:0x5, isstd:false, isutc:false},
{when:2026947600, index:0x4, isstd:false, isutc:false},
{when:2045696400, index:0x5, isstd:false, isutc:false},
{when:2058397200, index:0x4, isstd:false, isutc:false},
{when:2077146000, index:0x5, isstd:false, isutc:false},
{when:2090451600, index:0x4, isstd:false, isutc:false},
{when:2108595600, index:0x5, isstd:false, isutc:false},
{when:2121901200, index:0x4, isstd:false, isutc:false},
{when:2140045200, index:0x5, isstd:false, isutc:false},
},
cacheStart: 1396141200,
cacheEnd: 1414285200,
cacheZone: &time.zone{name:"BST", offset:3600, isDST:true},
},
},
~/.local/src/github.com/kr/pretty $ go test
--- FAIL: TestDiff-5 (0.00 seconds)
diff_test.go:49: diffing pretty.S{A: 0, S:(*pretty.S)(nil), I:interface {}(nil), C:[]int(nil)}
diff_test.go:50: with pretty.S{A: 0, S:(*pretty.S)(nil), I:interface {}(nil), C:[]int{ 1}}
diff_test.go:74: unexpected: C: []int(nil) != []int{1}
diff_test.go:74: missing: C: []int{} != []int{1}
--- FAIL: TestGoSyntax-5 (0.00 seconds)
formatter_test.go:99: expected "F(5)"
formatter_test.go:100: got "5"
formatter_test.go:101: expraw F(5)
formatter_test.go:102: gotraw 5
formatter_test.go:99: expected "pretty.LongStructTypeName{\n\tlongFieldName: pretty.LongStructTypeName{},\n\totherLongFieldName: \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP\" +\n\t\"QRSTUVWXYZ0123456789\",\n}"
formatter_test.go:100: got "pretty.LongStructTypeName{\n\tlongFieldName: %v(PANIC=reflect.Value.Interface: cannot return value obtained from unexported field or method)"
formatter_test.go:101: expraw pretty.LongStructTypeName{
longFieldName: pretty.LongStructTypeName{},
otherLongFieldName: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP" +
"QRSTUVWXYZ0123456789",
}
formatter_test.go:102: gotraw pretty.LongStructTypeName{
longFieldName: %v(PANIC=reflect.Value.Interface: cannot return value obtained from unexported field or method)
formatter_test.go:99: expected "&pretty.LongStructTypeName{\n\tlongFieldName: &pretty.LongStructTypeName{},\n\totherLongFieldName: (*pretty.LongStructTypeName)(nil),\n}"
formatter_test.go:100: got "&pretty.LongStructTypeName{\n\tlongFieldName: %v(PANIC=reflect.Value.Interface: cannot return value obtained from unexported field or method)"
formatter_test.go:101: expraw &pretty.LongStructTypeName{
longFieldName: &pretty.LongStructTypeName{},
otherLongFieldName: (*pretty.LongStructTypeName)(nil),
}
formatter_test.go:102: gotraw &pretty.LongStructTypeName{
longFieldName: %v(PANIC=reflect.Value.Interface: cannot return value obtained from unexported field or method)
formatter_test.go:99: expected "[]pretty.LongStructTypeName{\n\t{},\n\t{longFieldName:3, otherLongFieldName:3},\n\t{\n\t\tlongFieldName: \"abcdefghijklmnopqrstuvwxyzABCDEFGH\" +\n\t\t\"IJKLMNOPQRSTUVWXYZ0123456789\",\n\t\totherLongFieldName: <nil>,\n\t},\n}"
formatter_test.go:100: got "[]pretty.LongStructTypeName{\n\t{},\n\t{longFieldName:3, otherLongFieldName:3},\n\t{\n\t\tlongFieldName: %v(PANIC=reflect.Value.Interface: cannot return value obtained from unexported field or method),\n}"
formatter_test.go:101: expraw []pretty.LongStructTypeName{
{},
{longFieldName:3, otherLongFieldName:3},
{
longFieldName: "abcdefghijklmnopqrstuvwxyzABCDEFGH" +
"IJKLMNOPQRSTUVWXYZ0123456789",
otherLongFieldName: <nil>,
},
}
formatter_test.go:102: gotraw []pretty.LongStructTypeName{
{},
{longFieldName:3, otherLongFieldName:3},
{
longFieldName: %v(PANIC=reflect.Value.Interface: cannot return value obtained from unexported field or method),
}
formatter_test.go:99: expected "[]interface { }{\n\tpretty.LongStructTypeName{},\n\t[]byte{0x1, 0x2, 0x3},\n\tpretty.T{x:3, y:4},\n\tpretty.LongStructTypeName{\n\t\tlongFieldName: \"abcdefghijklmnopqrstuvwxyzABCDEFGH\" +\n\t\t\"IJKLMNOPQRSTUVWXYZ0123456789\",\n\t\totherLongFieldName: <nil>,\n\t},\n}"
formatter_test.go:100: got "[]interface {}{\n\tpretty.LongStructTypeName{},\n\t[]byte{0x1, 0x2, 0x3},\n\tpretty.T{x:3, y:4},\n\tpretty.LongStructTypeName{\n\t\tlongFieldName: %v(PANIC=reflect.Value.Interface: cannot return value obtained from unexported field or method),\n}"
formatter_test.go:101: expraw []interface { }{
pretty.LongStructTypeName{},
[]byte{0x1, 0x2, 0x3},
pretty.T{x:3, y:4},
pretty.LongStructTypeName{
longFieldName: "abcdefghijklmnopqrstuvwxyzABCDEFGH" +
"IJKLMNOPQRSTUVWXYZ0123456789",
otherLongFieldName: <nil>,
},
}
formatter_test.go:102: gotraw []interface {}{
pretty.LongStructTypeName{},
[]byte{0x1, 0x2, 0x3},
pretty.T{x:3, y:4},
pretty.LongStructTypeName{
longFieldName: %v(PANIC=reflect.Value.Interface: cannot return value obtained from unexported field or method),
}
FAIL
exit status 1
FAIL github.com/kr/pretty 0.004s
Input:
var thingA = map[int]int{
1: 2,
2: 3,
3: 4,
}
var thingB = map[int]int{
1: 1,
2: 2,
}
pretty.Fdiff(w, thingA, thingB)
Output:
[3]: "<int Value>" != (missing)[2]: 3 != 2[1]: 2 != 1
In larger maps, this makes the output really unreadable. Would you accept a pull request to put each difference on its own line?
Go (since 1.1) is well-known for having randomized map iteration order.
When iterating over a map with a range loop, the iteration order is not specified and is not guaranteed to be the same from one iteration to the next. Since Go 1 the runtime randomizes map iteration order, as programmers relied on the stable iteration order of the previous implementation.
https://blog.golang.org/go-maps-in-action
I actually use this kr/pretty
module to do a simple string compare and found that the ordering is randomized.
Parameters: {\"date\":\"2020-08-20\", \"digit\":\"123\"}
vs
Parameters: {\"digit\":\"123\", \"date\":\"2020-08-20\"}
I am here requesting an option for the user to choose if they want to iterate map in an deterministic order or not. (By default, it should be not deterministic as recommended practice by Go)
panic: reflect.Value.Interface: cannot return value obtained from unexported field or method [recovered]
panic: reflect.Value.Interface: cannot return value obtained from unexported field or method
goroutine 20 [running]:
testing.tRunner.func1(0xc8200ba3f0)
/usr/local/gosrc/src/testing/testing.go:450 +0x171
reflect.valueInterface(0x6d5a60, 0xc820222420, 0xe6, 0x1, 0x0, 0x0)
/usr/local/gosrc/src/reflect/value.go:912 +0xe7
reflect.Value.Interface(0x6d5a60, 0xc820222420, 0xe6, 0x0, 0x0)
/usr/local/gosrc/src/reflect/value.go:901 +0x48
github.com/kr/pretty.diffWriter.diff(0x7f6d2c25a078, 0xc820216440, 0xc82021a400, 0x10, 0x6d5a60, 0xc820222420, 0xe6, 0x6d5a60, 0xc8200a0bd0, 0xe6)
/root/go/src/github.com/kr/pretty/diff.go:63 +0x49b
github.com/kr/pretty.diffWriter.diff(0x7f6d2c25a078, 0xc820216440, 0xc82021a3d0, 0xc, 0x771a20, 0xc820222420, 0xd9, 0x771a20, 0xc8200a0bd0, 0xd9)
/root/go/src/github.com/kr/pretty/diff.go:87 +0x30cf
github.com/kr/pretty.diffWriter.diff(0x7f6d2c25a078, 0xc820216440, 0x7d5550, 0x9, 0x6c5660, 0xc8202101e0, 0xd7, 0x6c5660, 0xc82009a500, 0xd7)
/root/go/src/github.com/kr/pretty/diff.go:97 +0x2aff
github.com/kr/pretty.diffWriter.diff(0x7f6d2c25a078, 0xc820216440, 0x0, 0x0, 0x7839c0, 0xc8202101e0, 0xd9, 0x7839c0, 0xc82009a500, 0xd9)
/root/go/src/github.com/kr/pretty/diff.go:87 +0x30cf
github.com/kr/pretty.diffWriter.diff(0x7f6d2c25a078, 0xc820216440, 0x0, 0x0, 0x6b9240, 0xc8202101e0, 0x16, 0x6b9240, 0xc82009a500, 0x16)
/root/go/src/github.com/kr/pretty/diff.go:83 +0x1e51
github.com/kr/pretty.Fdiff(0x7f6d2c25a078, 0xc820216440, 0x6b9240, 0xc8202101e0, 0x6b9240, 0xc82009a500)
/root/go/src/github.com/kr/pretty/diff.go:25 +0x10e
github.com/kr/pretty.Diff(0x6b9240, 0xc8202101e0, 0x6b9240, 0xc82009a500, 0x0, 0x0, 0x0)
/root/go/src/github.com/kr/pretty/diff.go:19 +0xa0
Go 1.15 rc1 on Fedora Rawhide
Testing in: /builddir/build/BUILD/pretty-0.2.0/_build/src
PATH: /builddir/build/BUILD/pretty-0.2.0/_build/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin
GOPATH: /builddir/build/BUILD/pretty-0.2.0/_build:/usr/share/gocode
GO111MODULE: off
command: go test -buildmode pie -compiler gc -ldflags "-X github.com/kr/pretty/version=0.2.0 -extldflags '-Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld '"
testing: github.com/kr/pretty
github.com/kr/pretty
# github.com/kr/pretty
./formatter.go:40:9: conversion from int to string yields a string of one rune, not a string of digits (did you mean fmt.Sprint(x)?)
FAIL github.com/kr/pretty [build failed]
See golang/go#32479
package main
import (
"fmt"
"github.com/kr/pretty"
)
func main() {
type MyStruct struct {
S []MyStruct
str string
}
var s MyStruct
for i := 1; i <= 3; i++ {
s.S = append(s.S, MyStruct{})
}
slice2 := s.S
slice2 = append(slice2, MyStruct{str: "测试字符串"})
s.S[0].S = slice2
fmt.Printf("s.S:%v\n", pretty.Formatter(s.S))
}
it will fatal error: stack overflow
package main
import (
"fmt"
"github.com/kr/pretty"
)
type Foo struct {
S []string
}
func main() {
actual := Foo{
S: []string{"a", "b", "c"},
}
fmt.Println(pretty.Sprint(actual))
/* Prints:
main.Foo{
S: {"a", "b", "c"},
}
*/
// Would error:
/*
result := Foo{
S: {"a", "b", "c"},
}
*/
}
$ golint ./...
vendor/github.com/kr/pretty/diff.go:36:6: exported type Printfer should have comment or be unexported
vendor/github.com/kr/pretty/diff.go:47:6: exported type Logfer should have comment or be unexported
vendor/github.com/kr/pretty/diff.go:177:1: receiver name d should be consistent with previous receiver name w for diffPrinter
Bug in cycle detection reported in #95. Thank you @alingse. This issue is to track the bug itself as opposed to the fix. Description below copied from #95.
use visited map[visit]int
to check cycle might get some bad case.
the key reason was that, all the pp
, p
printer
share a same map[visit]int
but when we process a struct refer a pointer value twice or more, this is not a cycle, it's just a repeat refer.
maybe use other helper data type for every branch when we call code pp = *p
statement
maybe a slice
is enough to detect.
branch 1 : [v1, v2, v3, v5]
branch 2 : [v1, v2, v3, v4]
branch 3 : [v1, v2, v3, v4, v1]
only branch3 should print a "CYCLIC REFERENCE"
Executing the following code:
package main
import (
"fmt"
"github.com/kr/pretty"
)
type Foo struct {
name string
}
func main() {
fmt.Println(pretty.Diff(Foo{name: "foo"}, Foo{name: "bar"}))
}
Panics with the message:
panic: reflect.Value.Interface: cannot return value obtained from unexported field or method
I realize it may not be possible to actually produce the diff on private fields, but I doubt that a panic is what's intended.
Hi! I notice that this project doesn't have any tagged releases. Would you mind adding some SemVer-compatible release tags? It would really, really help those of us using dep
and similar tools.
Hi there, thanks for making this package.
I observed this issue printing a complex data structure with several structs containing interface
fields which could be one of many conforming types. A number of those types might be empty structs, where the type is the only relevant information (the zero value is the only value).
If the outer struct with an interface
field is printed, the nonzero
function returns false
because the concrete value of the interface field is zero. An example playground showing this can be found here: https://go.dev/play/p/HjxnCorTQtj
For interface
fields, would it be possible to mark the field as nonzero if the field is non-nil, rather than the value of what the field is pointing at?
Thanks!
package main
import (
"fmt"
"github.com/kr/pretty"
)
func main() {
// This should print the zero value for A
v := &A{Err: nil}
pretty.Println(v)
// This will print &main.FormatError{...}
v.Err = &FormatError{"foo.go", 12}
pretty.Println(v)
// This SHOULD print &main.A{Err: main.LogicError{}}, but prints the zero value for A instead.
v.Err = LogicError{}
pretty.Println(v)
}
type A struct {
Err error
}
type FormatError struct {
File string
Line int
}
func (err *FormatError) Error() string {
return fmt.Sprintf("format error: %s:%d", err.File, err.Line)
}
type LogicError struct{}
func (LogicError) Error() string {
return "logic error"
}
&main.A{}
&main.A{
Err: &main.FormatError{File:"foo.go", Line:12},
}
&main.A{}
&main.A{}
&main.A{
Err: &main.FormatError{File:"foo.go", Line:12},
}
&main.A{
Err: &main.LogicError{},
}
in function func (w diffPrinter) diff(av, bv reflect.Value)
at := av.Type()
bt := bv.Type()
if at != bt{
can this at be compared with bt directly? I suspect it should be at.String() != bt.String()
The current library uses Go language verion 1.12.
go 1.12
This version is very old.
Version 1.12 was released in February 2019 according to the Go's website, https://go.dev/project.
Today is February 2023. Four (4) years have passed !
Thank you.
Would you accept a pull request for
func Sprint(a ...interface{}) string
?
As this library is related to visual output, it'd be cool to see what you're going to get before downloading and trying it out.
I use this package regularly across several projects - thanks for building it!
Would it be possible to add a way to control the max depth? At the moment I see it is set to 10 but sometimes I want more:
Hi @kr
Would you like to make a tagged release? The latest release is tagged at the year 2020.
Same request as google/go-cmp#185, where the conclusion was to print all unsigned integers in decimal form, except uint8 and uintptr which would be printed in hex (see this change).
I think diff should have the same support as well.
package main
import (
"fmt"
"reflect"
"github.com/kr/pretty"
)
func main() {
var s1 []int
s2 := []int{}
fmt.Println(pretty.Diff(s1, s2))
fmt.Printf("Is %#v deep equal to %#v ? %v\n", s1, s2, reflect.DeepEqual(s1, s2))
}
output
[]
Is []int(nil) deep equal to []int{} ? false
package main
import (
"fmt"
"github.com/kr/pretty"
)
type A struct {
B *B
}
type B struct {
M map[string]string
}
func main() {
a := &A{B: &B{M: map[string]string{}}}
fmt.Printf("%# v\n", pretty.Formatter(a))
//prints out
/*
&main.A{
B: &main.B{
M: {},
},
}
*/
//This contains a syntax error
c := &A{
B: &B{
M: {},
},
}
}
Is it currently possible to get Formatter
to use an object's String()
when such a method is present? Would you consider a pr that added a new formatter that did this?
~/src/golang.org/x/build$ go list -m github.com/kr/pretty
github.com/kr/pretty v0.1.0
~/src/golang.org/x/build$ go version
go version devel +55931b5e16 Mon Sep 24 12:55:24 2018 -0400 linux/amd64
~/src/golang.org/x/build$ go test github.com/kr/pretty
--- FAIL: TestGoSyntax (0.00s)
formatter_test.go:170: expected "float64(1)"
formatter_test.go:171: got "float64(1.0)"
formatter_test.go:172: expraw
float64(1)
formatter_test.go:173: gotraw
float64(1.0)
formatter_test.go:170: expected "(1+0i)"
formatter_test.go:171: got "(1.0+0.0i)"
formatter_test.go:172: expraw
(1+0i)
formatter_test.go:173: gotraw
(1.0+0.0i)
FAIL
FAIL github.com/kr/pretty 0.019s
On weekly.2012-02-07, I'm seeing an error when trying to print out a http.Client
type.
package main
import "fmt"
import "github.com/kr/pretty.go"
import "net/http"
func main() {
fmt.Printf("%# v\n", pretty.Formatter(new(http.Client)))
}
The following is printed when the program is executed.
&%v(PANIC=runtime error: comparing uncomparable type func(*http.Request, []*http.Request) error)
Hi,
I have a feature request: When looking for differences between structures that contain floating point numbers it is often nice to not require exact matches, i.e. one can often treat the the values as equal if they are within say 1% of each other. It would be nice if the diff interface could support this kind of fuzzy matching.
go: could not create module cache: mkdir /usr/local/go/bin/pkg: permission denied
**this is only a problem with the dependency compilation in goland and it compiles and runs fine in command line
using go run *.go
I think it has something to do with the dependency management not finding this repo.
When I run the following short script with Go 1.17 it prints a PANIC=Format method
message
package main
import (
"github.com/kr/pretty"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func main() {
o := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
}
pretty.Println(o)
}
$ go1.16.1 run ./
v1.Secret{
TypeMeta: v1.TypeMeta{},
ObjectMeta: v1.ObjectMeta{
Name: "foo",
GenerateName: "",
Namespace: "",
SelfLink: "",
UID: "",
ResourceVersion: "",
Generation: 0,
CreationTimestamp: v1.Time{},
DeletionTimestamp: (*v1.Time)(nil),
DeletionGracePeriodSeconds: (*int64)(nil),
Labels: {},
Annotations: {},
OwnerReferences: nil,
Finalizers: nil,
ClusterName: "",
ManagedFields: nil,
},
Immutable: (*bool)(nil),
Data: {},
StringData: {},
Type: "",
}
$ go1.17 run ./
v1.Secret{
%!v(PANIC=Format method: runtime error: invalid memory address or nil pointer dereference)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.