Why is Go PANICking?

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

相关文章

什么是Go的接口? 接口可以说是一种类型,可以粗略的理解为他...
1、Golang指针 在介绍Golang指针隐式间接引用前,先简单说下...
1、概述 1.1 Protocol buffers定义 Protocol buffe...
判断文件是否存在,需要用到"os"包中的两个函数: os.Stat(...
1、编译环境 OS :Loongnix-Server Linux release 8.3 CPU指...
1、概述 Golang是一种强类型语言,虽然在代码中经常看到i:=1...