duanjia1870 2017-03-09 15:31 采纳率: 0%
浏览 105

使用panic替换golang错误代码以进行所有错误处理

We have medium sized application written in go. From all the code lines about 60 percent goes to code error handling. Something like this:

if err != nil {
   return err
}

After some time, writing this lines over and over again becomes tiresome and we are now thinking to replace all error codes with panics.

I know panics aren't meant to be used like that.

What can be potential pitfall and does anyone have experience with something similar ?

  • 写回答

2条回答 默认 最新

  • douxian6008 2017-03-09 20:59
    关注

    The main pitfall would be a widespread use of hammers to drive screws. Panic is for unrecoverable/unexpected errors, error return values are for recoverable/expected errors.

    Replace the word "panic" with "crash", because that is conceptually what a panic is. Do you honestly want to write an application that by design crashes whenever anything goes in any way remotely wrong? That would be the most fragile application on Earth, the very antithesis of fault tolerance.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)