doumeinuoye81969 2019-04-02 09:12
浏览 729
已采纳

是否所有Golang函数都将err作为第二个返回值返回

I am new a Golang programmer and I see code snippets were functions return err as last argument a lot of time.

f, err := strconv.ParseFloat(asciiFloat, 64)

Question is must all functions be made to return err even if no error handling is involved in the function implementation. And how will I know a function is or is not returning an err value?

Must I checkout docs every time I need to use a function?

  • 写回答

2条回答 默认 最新

  • dongliyi967823 2019-04-02 10:32
    关注

    Answering your question: Fortunately, Go prevents certain types of programmers errors. It just won't let you compile the program if you forget one of the values that the function returns.

    It's a good practice to return errors in Go, read Errors section of Effective Go

    Library routines must often return some sort of error indication to the caller. As mentioned earlier, Go's multivalue return makes it easy to return a detailed error description alongside the normal return value. It is good style to use this feature to provide detailed error information.

    and Error handling and Go

    In Go, error handling is important. The language's design and conventions encourage you to explicitly check for errors where they occur

    However there is a problem described here and probably will be improved in Go 2

    In general Go programs have too much code checking errors and not enough code handling them.

    Dave Cheney writes in the blog

    I’m suggesting changing your code so you don’t have as many errors to handle.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格