当前位置:网站首页>Original error interface
Original error interface
2022-06-12 17:37:00 【Erya preaches Buddhism】
1, summary .
Go The language introduces a standard pattern for error handling , namely error Interface , It is Go The type of interface built into the language , This interface is defined as follows :
type error interface {
Error() string
}
Go Standard library code package for languages errors Provide users with the following methods :
package errors
type errorString struct {
text string
}
func New(text string) error {
return &errorString{text}
}
func (e *errorString) Error() string {
return e.text
}
The other can generate error The method of type value is to call fmt In bag Errorf function :
package fmt
import "errors"
func Errorf(format string, args ...interface{}) error {
return errors.New(Sprintf(format, args...))
}
2,error Use of interfaces .
1, A simple example .
package main
import "fmt"
import "errors"
func main() {
// First use fmt Bad method in package
//var err1 error = fmt.Errorf("s%","this is normal err1") Equivalent to the following
err1 := fmt.Errorf("%s", "this is normal err1")
fmt.Println("err1 = ", err1)
// Or use it directly error package
err2 := errors.New("this is normal err2")
fmt.Println("err2 = ", err2)
}
2, How to apply .
package main
import "fmt"
import "errors"
func MyDiv(a, b int) (result int, err error) {
err = nil
if b == 0 {
err = errors.New(" The denominator cannot be 0")
} else {
result = a / b
}
return
}
func main() {
result, err := MyDiv(10, 0)
if err != nil {
fmt.Println("err = ", err)
} else {
fmt.Println("result = ", result)
}
}
边栏推荐
- MySQL authorization summary
- TensorFlow2训练数据集的两种方式
- 认识函数原创
- Yyds dry goods inventory leetcode question set 911 - 920
- Figma从入门到放弃
- 三代DRI的变化
- ssm常用到的依赖
- Project training of Software College of Shandong University - Innovation Training - network attack and defense shooting range experimental platform of Software College of Shandong University (XXV) - p
- R语言使用epiDisplay包的tabpct函数生成二维列联表并使用马赛克图可视化列联表(二维列联表、边际频数、以及按行、按列的比例)、自定义设置cex.axis参数改变轴标签数值的大小
- 龙芯处理器内核中断讲解
猜你喜欢
Swintransformer network architecture
布局管理中的sizePolicy的策略问题
DRM 驱动 mmap 详解:(一)预备知识
Memory control of node
JDBC几个坑
Arm64 stack backtracking
TensorFlow求梯度时提示TypeError: unsupported operand type(s) for *: ‘float‘ and ‘NoneType‘
Exclusive interview with oppo find X5 Product Manager: deeply cultivate self-developed chips to create the ultimate flagship experience with the highest standards
Atlas conflict Remote Code Execution Vulnerability (cve-2022-26134) vulnerability recurrence
Compilation optimization of performance optimization
随机推荐
To understand Devops, you must read these ten books!
新媒体运营素材网站分享,让你创作时事半功倍
Is it cost-effective to apply for Huagui sweet home term life insurance? What are the advantages of this product?
全局锁、表锁、行锁
多种Qt的开发方式,你选择哪种?
MySQL transaction introduction and transaction isolation level
Crazy temporary products: super low price, big scuffle and new hope
Microsoft Office MSDT Code Execution Vulnerability (cve-2022-30190) vulnerability recurrence
How to change Golan back to the English version when it becomes the Chinese version
R语言计算data.table在一个分组变量的值固定的情况下另外一个分组变量下指定数值变量的均值
Detailed explanation of shardingjdbc database and table
电控学习 第二周
First acquaintance with go language
Evolution and thinking of Taobao native R & D mode | DX R & D mode
How to view, modify, and delete SSH
php 实现无限极分类树(递归及其优化)
bug记录:更新数据库时报错:Data truncation: Incorrect datetime value:
认识函数原创
R语言使用epiDisplay包的tabpct函数生成二维列联表并使用马赛克图可视化列联表(二维列联表、边际频数、以及按行、按列的比例)、自定义设置ylab参数设置Y轴的轴标签文本(y axis)
Add static route