The default structure for error messages in golang contains a string but I want to add dynamic response code and the time the error occurred to it. Any suggestions on how to do it?
1条回答 默认 最新
- dongzaijiao4863 2017-05-03 04:57关注
error
in not a struct, is an interface.type error interface { Error() string }
you can define your own error struct, just implement the
Error() string
function.type ErrorA struct { // any field you want } func (e ErrorA) Error() string { // implement this function }
then
ErrorA
can be used aserror
.ref:
1. https://golang.org/ref/spec#Errors
2. https://golang.org/ref/spec#Interface_types本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报