码迷,mamicode.com
首页 > 其他好文 > 详细

Why is Go PANICking?

时间:2021-03-05 13:31:10      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:https   lan   ali   star   from   env   mes   trace   message   

A panic should always be a last resort, and even then consider a better option!

  • Logging errors with context (cause and message)
  • Expose errors as metrics
  • Expose errors as events

So after all the long talk, when is it okay to panic?

  • Panics are somewhat okay when the error state needs attention and there’s no going forward from there.

  • An example would be starting an application with a missing environment variable or having an invalid configuration (this could also be hot reloaded).

  • No amount of error handling would fit a case of this, panic as needed and let the user know their attention is needed. A failed write to a store could be worth a panic as the application not writing will lead to a fatal inconsistent state etc.

  • A lot of the time, panics are needed only when you have a fatal end and need to stop to save yourself, rather than shoot yourself in the foot for some fancy stack trace.

To end this, I say:

With great power comes great responsibility, but even Spiderman knew better than to panic unless needed.

The end!

https://tiemma.medium.com/why-is-go-panicking-31ba2351986b

Why is Go PANICking?

标签:https   lan   ali   star   from   env   mes   trace   message   

原文地址:https://www.cnblogs.com/CherryTab/p/14485157.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!