5

Go 语言错误处理的姿势

 2 years ago
source link: https://www.v2ex.com/t/805771
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Go 编程语言

Go 语言错误处理的姿势

  nanmu42 · 2 小时 9 分钟前 · 251 次点击

前段时间看到 v2 上有个帖子,询问如何在 Go 中为错误加上堆栈,我以前也为类似的问题困扰过,后来找到了 pkg/errors ,再后来官方库有了 fmt.Errorf() ,我把这个小小经验写了下来,希望能抛砖引玉,欢迎各位交流拍砖。

在这篇文章中,我们将区分错误( error )和异常( panic ),讨论什么样的错误是“好”的(容易检查和排错),介绍一种让错误变“好”的常用方式(fmt.Errorf())。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK