In Golang, we return errors explicitly using the return statement. This contrasts with the exceptions used in languages like java, python. The approach in Golang to makes it easy to see which function returns an error?

在Golang中,我们使用return语句显式返回错误。 这与Java,Python等语言中使用的例外形成对比。 Golang中的方法可以轻松查看哪个函数返回错误

In Golang, errors are the last return value and have type error, a built-in interface.

在Golang中,错误是最后一个返回值,其错误类型为内置接口。

errors.New() is used to construct basic error value with the given error messages.

errors.New()用于根据给定的错误消息构造基本错误值。

We can also define custom error messages using the Error() method in Golang.

我们还可以使用Golang中的Error()方法定义自定义错误消息

How to create an error message in Golang?

如何在Golang中创建错误消息?

Syntax:

句法:

    err_1 := errors.New("Error message_1: ")
err_2 := errors.New("Error message_2: ")

Basic program to test an error in Golang

测试Golang错误的基本程序

package main
import (
"fmt"
"errors"
)
func test(value int) (int, error) {
if (value == 0) {
return 0, nil;
} else {
return -1, errors.New("Invalid value: ")
}
}
func main() {
value, error := test(10)
fmt.Printf("Value: %d, Error: %v", value, error)
value, error = test(0)
fmt.Printf("\n\nValue: %d, Error: %v", value, error)
}

Output

输出量

Value: -1, Error: Invalid value:
Value: 0, Error: <nil>

翻译自: https://www.includehelp.com/golang/how-to-return-an-error-in-golang.aspx

如何在Golang中返回错误?相关推荐

  1. 如何在 Swift 中进行错误处理

    作者:Olivier Halligon,原文链接,原文日期:2015-12-17 译者:JackAlan:校对:靛青K:定稿:Channe 今天的文章讲解如何在 Swift 中进行错误处理. 说实话, ...

  2. 在ASP.NET Web API中返回错误的最佳实践

    本文翻译自:Best practice to return errors in ASP.NET Web API I have concerns on the way that we returns e ...

  3. 如何在Mercurial中编辑错误的提交消息? [重复]

    本文翻译自:How to edit incorrect commit message in Mercurial? [duplicate] This question already has answe ...

  4. 如何在golang中关闭bufio.reader_Golang 并发模型系列:1. 轻松入门流水线模型

    Go语言中文网,致力于每日分享编码.开源等知识,欢迎关注我,会有意想不到的收获! Golang作为一个实用主义的编程语言,非常注重性能,在语言特性上天然支持并发,它有多种并发模型,通过流水线模型系列文 ...

  5. C语言中返回错误信息的函数总结

    C语言strerror()函数:返回错误原因的描述字符串 头文件: #include <string.h> 定义函数: char * strerror(int errnum); 函数说明: ...

  6. golang判断结构体为空_如何在Golang中检查结构是否为空?

    golang判断结构体为空 The size of an empty structure is zero in Golang. Here, empty structure means, there i ...

  7. Java错误提示框口怎么使用_如何在Swing中显示错误消息对话框?

    以下示例展示了如何在基于swing的应用程序中显示错误消息警告. 使用以下API - JOptionPane - 创建标准对话框. JOptionPane.showMessageDialog() - ...

  8. java 同时返回两个参数,如何在java中返回两个参数

    >例如,使用Container类 public class FirstAndLastName { String firstName; String lastName; ... // constr ...

  9. Golang中的自动伸缩和自防御设计

    Raygun服务由许多活动组件构成,每个组件用于特定的任务.其中一个模块是用Golang编写的,负责对iOS崩溃报告进行处理.简而言之,它接受本机iOS崩溃报告,查找相关的dSYM文件,并生成开发者可 ...

最新文章

  1. intellijidea课程 intellijidea神器使用技巧 3-1 列操作
  2. unity game和scene效果不一样_都说水泥漆和乳胶漆只是叫法不一样,对房子装修效果一样,事实呢...
  3. 程序员面试题精选100题(58)-八皇后问题[算法]
  4. 方法区如何判断是否需要回收
  5. 技术讨论 | 一次尚未完成的薅羊毛行动
  6. 用strace工具跟踪系统调用
  7. 异常解析————Parameter metadata not available for the given statement
  8. nginx+tomcat+redis实现session共享
  9. 使用嵌套类/临时类保存数据
  10. Anaconda各版本对应的python版本
  11. php goeasy,PHP使用GOEASY实现WEB实时推送
  12. 详解Autosar Arxml中的CANFD报文及格式
  13. CAD图纸中图块无法打开编辑?
  14. matlab如何根据历年gdp找增长规律,人均gdp增长率_中国历年gdp数据图解 中国历年gdp增长率及人均GDP 1978年 2016年...
  15. 黑鲨重装计算机安装无法继续,示例黑鲨装机大师装机失败无法开机怎么办?
  16. 便利蜂创始人数字化经验分享:如何用全链路数字化 重塑零售业
  17. 幅频特性曲线protues_频率响应,幅频特性,增益与相位裕量,波特图
  18. deepin-wine下微信升级到最新版方法
  19. 嘿,妈妈,我是设计师
  20. 这世界无非是“人人为我,我为人人”

热门文章

  1. html中文字过长 自动隐藏,css 实现文字过长自动隐藏功能
  2. 批处理停止php服务,通过批处理启动和停止MSSQL+IIS
  3. Mysql Incorrect DATETIME value: ‘01/01/2021 00:00:00‘
  4. flex弹性布局操练2
  5. css的三个特性 背景透明设置
  6. Access-Ctrol-Allow-Headers:*兼容问题导致的跨域失败
  7. 信息的Raid存储方式,更安全的保障,更花钱的保障!
  8. [leetcode-108,109] 将有序数组转换为二叉搜索树
  9. django QuerySet对象转换成字典对象
  10. 洛谷-P1160 队列安排