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本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 求高通平台Softsim调试经验
- ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
- ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
- ¥15 栅极驱动低侧烧毁MOSFET
- ¥30 写segy数据时出错3
- ¥100 linux下qt运行QCefView demo报错
- ¥50 F1C100S下的红外解码IR_RX驱动问题
- ¥20 基于matlab的航迹融合 航迹关联 航迹插补
- ¥15 用Matlab实现图中的光线追迹
- ¥15 联想笔记本开机出现系统更新界面