英文:
How to implement both fmt.Stringer and error interface on a type?
问题
与 fmt.Stringer 类似,fmt 包在打印值时会查找 error 接口。如果一个 类型 同时满足这两个接口,fmt 只会查找 error 而不是 Stringer。为什么会这样?我如何在一个类型上同时实现 Stringer 和 error 接口?
英文:
As with fmt.Stringer, the fmt package looks for the error interface when printing values. In case if a type satisfies both interfaces, fmt only on look for error not Stringer. Why? How I could implement both Stringer and error on a type?
答案1
得分: 2
并不是一个通用解决方案,但在某些情况下,可以使用指针和非指针接收器来实现这个技巧:https://play.golang.org/p/DY4Cq4efsT
英文:
Not really a universal solution, but in certain situations one can use pointer vs non-pointer receivers to do the trick: https://play.golang.org/p/DY4Cq4efsT
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论