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?
关注
码龄 粉丝数 原力等级 --
- 被采纳
- 被点赞
- 采纳率

首页/
已采纳
在Golang中自定义错误结构
收起
- 写回答
- 好问题 0 提建议
- 关注问题
微信扫一扫
点击复制链接分享
- 邀请回答
- 编辑 收藏 删除 结题
- 收藏 举报
1条回答 默认 最新
- 关注
码龄 粉丝数 原力等级 --
- 被采纳
- 被点赞
- 采纳率
dongzaijiao4863 2017-05-02 20: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本回答被题主选为最佳回答 , 对您是否有帮助呢? 本回答被专家选为最佳回答 , 对您是否有帮助呢? 本回答被题主和专家选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏举报微信扫一扫
点击复制链接分享
编辑预览轻敲空格完成输入- 显示为
- 卡片
- 标题
- 链接
评论按下Enter换行,Ctrl+Enter发表内容
编辑
预览

轻敲空格完成输入
- 显示为
- 卡片
- 标题
- 链接
报告相同问题?
提交
- 2017-08-22 04:26回答 2 已采纳 Yes, you can fetch the precise structure of the type, if that's what you mean with "root type": v
- 2019-01-22 19:52回答 2 已采纳 You have to differentiate between the "static type" and the "dynamic type" of a variable. Each an
- 2019-05-28 23:29回答 2 已采纳 Unfortunately or not, there is no way to do this in Go. First tool that comes to mind is reflectio
- 2020-09-18 20:52### goLang引入自定义包的方法 #### 一、设置$GOPATH环境变量 在Go语言中,为了确保项目能够正确地被编译器识别并管理,我们需要设置`$GOPATH`环境变量。这个变量指向一个目录,该目录包含了Go的工作空间,其中...
- 2019-09-04 07:47回答 1 已采纳 Those are not embedded; embedding has a specific meaning in Go, as outlined in the spec, and as ex
- 2019-06-10 18:39回答 1 已采纳 Remove the "&" before msg. msg is already a pointer. This may make problems. Your question is not
- 2017-05-09 19:26回答 2 已采纳 As @zzn said, it's not possible to refer to a variable in one template from a different one. One
- 2020-09-19 03:15在Golang编程中,错误处理是一个重要的方面,但过度的错误检查可能导致代码变得冗余且难以阅读。本篇文章将探讨如何优化Golang中的重复错误处理,以提高代码的可读性和性能。 首先,我们来看一个典型的错误处理模式...
- 2019-03-02 18:14回答 2 已采纳 Your logic assumes that the length of a is always the proper count to conditionally iterate throug
- 2014-07-17 21:14回答 1 已采纳 Read this: http://golang.org/doc/code.html — but in short: One package per directory/one directo
- 2017-04-25 00:47回答 1 已采纳 There's no standard way to do this. The most obvious way, which should only be used if no other m
- 2024-06-06 01:57自由软件开发者的博客 5.在函数中抛出自定义错误结构。4.为自定义错误结构实现方法。2. 在函数中返回错误。
- 2022-12-18 01:06Ding Jiaxiong的博客 非零基础自学Golang 第10章 错误处理 10.1 错误处理的方式 & 10.2 自定义错误
- 2017-05-15 03:28大戴丶的博客 第一次尝试自定义包,是在帮自己的室友配Go环境的时候。当时配完环境之后,第一件事就是"Hello world"这可能是世界通用的编程输出语句吧。我突发奇想,想把这句"Hello world"放在另外一个包中,通过跨包
- 2024-10-28 08:05FightForY的博客 gin框架中使用到了validator包,但是没有自定义通义的错误信息的地方。故需要通过反射自行实现。
- 没有解决我的问题, 去提问
联系我们(工作时间:8:30-22:00)
400-660-0108kefu@csdn.net在线客服
- 京ICP备19004658号
- 经营性网站备案信息
公安备案号11010502030143
- 营业执照
- 北京互联网违法和不良信息举报中心
- 家长监护
- 中国互联网举报中心
- 网络110报警服务
- Chrome商店下载
- 账号管理规范
- 版权与免责声明
- 版权申诉
- 出版物许可证
- ©1999-2025北京创新乐知网络技术有限公司