当前位置:网站首页>Go 语言怎么优化重复的 if err != nil 样板代码?
Go 语言怎么优化重复的 if err != nil 样板代码?
2022-07-01 16:11:00 【frank.】
大家好,我是 frank。
01
介绍
Go 语言的错误处理在社区中一直被部分 Go 开发者诟病,其中重复的 if err != nil 样板代码是最令 Go 开发者难以忍受的,严重降低代码的可读性。
针对这个社区争论非常激烈的话题,笔者介绍两种优化重复的 if err != nil 样板代码的方式。
02
封装错误检查函数
错误检查函数示例代码:
func CheckErrors (err error) {
if err != nil {
// do something
}
}
调用错误检查函数示例代码:
err := SomeMethod()
CheckErrors(err)
阅读上面这段代码,封装一个错误检查的函数,代码中需要处理错误的地方,直接调用该函数。
这种方式,虽然在视觉上提升了代码的可读性,但是在需要特殊处理错误的场景也有局限性,比如需要使用额外的信息完善错误时,该方式并不适用此类场景。
03
结构体中定义错误信息字段
我们可以在结构体中定义错误信息字段,将结构体的方法与错误信息绑定在一起。
示例代码:
type Writer struct {
err error
buf []byte
n int
wr io.Writer
}
func (b *Writer) Flush() error {
if b.err != nil {
return b.err
}
// ...
return nil
}
阅读 Go 标准库 bufio 的代码片段,我们可以发现在 Writer 结构体中定义一个 err 字段,将错误信息封装在结构体中。
在 Writer 结构体的方法的开头先判断 err 字段是否为 nil,如果 err 字段的值不是 nil,则直接返回 err,从而减少 if err != nil 样板代码的重复出现。
04
总结
本文我们介绍怎么优化 Go 语言中重复的 if err != nil 样板代码的两种方式,笔者推荐使用第二种方式,因为第一种方式虽然简单,但是在一些特定场景并不适合。
通过在结构体中定义错误信息的字段,将结构体的方法和错误信息绑定在一起的优化方式,相比较第一种方式更加优雅。
参考资料:
- https://www.reddit.com/r/golang/comments/6v07ij/copypasting_if_err_nil_return_err_everywhere/
- https://www.reddit.com/r/golang/comments/649o0c/syncx_go_library_that_extends_standard_sync/
- https://go.dev/blog/errors-are-values
- https://pkg.go.dev/golang.org/x/sync/errgroup
- https://pkg.go.dev/github.com/facebookgo/stackerr#section-readme
边栏推荐
- Sqlserver query: when a.id is the same as b.id, and the A.P corresponding to a.id cannot be found in the B.P corresponding to b.id, the a.id and A.P will be displayed
- Trace the source of drugs and tamp the safety dike
- The picgo shortcut is amazing. This person thinks exactly the same as me
- 圈铁发音,动感与无噪强强出彩,魔浪HIFIair蓝牙耳机测评
- 马来西亚《星报》:在WTO MC12 孙宇晨仍在坚持数字经济梦想
- Automatic, intelligent and visual! Deeply convinced of the eight designs behind sslo scheme
- 投稿开奖丨轻量应用服务器征文活动(5月)奖励公布
- 苹果自研基带芯片再次失败,说明了华为海思的技术领先性
- 【SQL语句】请问这边为什么select出了两个上海,查询出了不同的count我想让他变成一个上海,count只显示一个总和
- How does win11 set user permissions? Win11 method of setting user permissions
猜你喜欢

Hardware development notes (9): basic process of hardware development, making a USB to RS232 module (8): create asm1117-3.3v package library and associate principle graphic devices

process. env. NODE_ ENV

揭秘慕思“智商税”:狂砸40亿搞营销,发明专利仅7项

C#/VB. Net merge PDF document

嵌入式开发:5个修订控制最佳实践

马来西亚《星报》:在WTO MC12 孙宇晨仍在坚持数字经济梦想

制造业数字化转型究竟是什么

SQLServer查询: a.id与b.id相同时,a.id对应的a.p在b.id对应的b.p里找不到的话,就显示出这个a.id和a.p

AVL 平衡二叉搜索树

picgo快捷键 绝了这人和我的想法 一模一样
随机推荐
IM即时通讯开发万人群聊消息投递方案
【Hot100】19. Delete the penultimate node of the linked list
MySQL的零拷贝技术
从大湾区“1小时生活圈”看我国智慧交通建设
开机时小键盘灯不亮的解决方案
【Hot100】17. Letter combination of telephone number
Sqlserver query: when a.id is the same as b.id, and the A.P corresponding to a.id cannot be found in the B.P corresponding to b.id, the a.id and A.P will be displayed
瑞典公布决定排除华为5G设备,但是华为已成功找到新出路
Win11如何設置用戶權限?Win11設置用戶權限的方法
The latest NLP game practice summary!
超视频时代,什么样的技术会成为底座?
There will be a gap bug when the search box and button are zoomed
Pico,是要拯救还是带偏消费级VR?
周少剑,很少见
[IDM] IDM downloader installation
使用腾讯云搭建图床服务
Talking from mlperf: how to lead the next wave of AI accelerator
2022-07-01日报:谷歌新研究:Minerva,用语言模型解决定量推理问题
How to adjust the size of computer photos to what you want
Hardware development notes (9): basic process of hardware development, making a USB to RS232 module (8): create asm1117-3.3v package library and associate principle graphic devices