当前位置:网站首页>Go language learning notes - exception handling | go language from scratch
Go language learning notes - exception handling | go language from scratch
2022-04-23 09:18:00 【Swordsman a Liang_ ALiang】
Catalog
1、 Use to implement Error The interface way
2、 Use errors Method to build exception object
Learning notes , Where is where .
Go The language has a built-in error interface , Exceptions can be defined by implementing interfaces , And other languages try···catch The grammar is different . I look very uncomfortable now .
Built in error The interface is as follows :
type error interface {
Error() string
}
1、 Use to implement Error The interface way
The sample code is as follows
package main
import "fmt"
type CustomError struct {
code int
msg string
}
func (error *CustomError) Error() string {
return fmt.Sprintf("Error -> code=%d,msg=%s\n", error.code, error.msg)
}
func test_intf(a, b int) (result int, errorMsg string) {
if a > b {
return a, ""
} else {
_error := CustomError{1, "a <= b"}
return 0, _error.Error()
}
}
func main() {
result, msg := test_intf(10, 20)
if result == 0 {
fmt.Println(msg)
} else {
fmt.Printf("result = %d\n", result)
}
}
Execution results
Error -> code=1,msg=a <= b
Be careful
1、 Build a custom exception structure CustomError, Realization Error Method .
2、 Use errors Method to build exception object
The sample code is as follows
package main
import (
"errors"
"fmt"
)
var errCust = errors.New(" Custom exception ")
func test_cust(a, b int) (int, error) {
if b == 0 {
return 0, errCust
}
return a / b, nil
}
func main() {
if res, err := test_cust(10, 0); err != nil {
fmt.Printf("test_cust error is %v\n", err)
} else {
fmt.Printf("test_cust is %d\n", res)
}
}
Execution results
test_cust error is Custom exception
Summary
It feels like something is missing , I'll think about the learning route later , You need to stop and reflect .
版权声明
本文为[Swordsman a Liang_ ALiang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230633437566.html
边栏推荐
- Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
- Using JS to realize a thousandth bit
- OpenCV中的图像处理 —— 轮廓入门+轮廓特征
- npm ERR! network
- Matlab draw five-star red flag
- Arbre de dépendance de l'emballage des ressources
- BK3633 规格书
- Go language self-study series | golang structure as function parameter
- Four pictures to understand some basic usage of Matplotlib
- Little girl walking
猜你喜欢

Kettle experiment (III)

Strength comparison vulnerability of PHP based on hash algorithm

EmuElec 编译总结

Download and install bashdb

Multi view depth estimation by fusing single view depth probability with multi view geometry

Kettle experiment conversion case

《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路
![[C language] document operation](/img/89/b19dda13d27e37fedf6736c102245b.png)
[C language] document operation

Write down the post order traversal of the ~ binary tree

How to protect open source projects from supply chain attacks - Security Design (1)
随机推荐
Kettle experiment (III)
Little girl walking
Chapter VIII project stakeholder management of information system project manager summary
LGB, XGB, cat, k-fold cross validation
112. 路径总和
错题汇总1
The crawler returns null when parsing with XPath. The reason why the crawler cannot get the corresponding element and the solution
Kettle实验
Resource packaging dependency tree
Error: cannot find or load main class
OpenCV中的图像处理 —— 轮廓入门+轮廓特征
Notes on xctf questions
Matlab draw five-star red flag
机器学习(六)——贝叶斯分类器
Non duplicate data values of two MySQL query tables
员工试用期转正申请书(泸州老窖)
Number of islands
Flink同时读取mysql与pgsql程序会卡住且没有日志
Detailed explanation of delete, truncate and drop principles in MySQL database
AQS & reentrantlock implementation principle