当前位置:网站首页>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
边栏推荐
- [in-depth good article] detailed explanation of Flink SQL streaming batch integration technology (I)
- Trc20 fund collection solution based on thinkphp5 version
- Employee probation application (Luzhou Laojiao)
- 653. 两数之和 IV - 输入 BST
- Number of islands
- Mini - exercice MySQL (seulement pour les débutants, pas pour les non - débutants)
- 2021 Li Hongyi's adaptive learning rate of machine learning
- Chapter VIII project stakeholder management of information system project manager summary
- Emuelec compilation summary
- 【原创】使用System.Text.Json对Json字符串进行格式化
猜你喜欢

Principle of synchronized implementation

《信息系统项目管理师总结》第八章 项目干系人管理

Kettle实验 (三)

Kettle实验

Vivo, hardware safe love and thunder

DJ music management software pioneer DJ rekordbox

MySQL small exercise (only suitable for beginners, non beginners are not allowed to enter)

AQS & reentrantlock implementation principle

Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“

The crawler returns null when parsing with XPath. The reason why the crawler cannot get the corresponding element and the solution
随机推荐
Valgrind et kcachegrind utilisent l'analyse d'exécution
SQL used query statements
Detailed explanation of delete, truncate and drop principles in MySQL database
653. 两数之和 IV - 输入 BST
Applet error: should have URL attribute when using navigateto, redirectto or switchtab
Valgrind and kcache grind use run analysis
错题汇总1
Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands
I don't understand time, timestamp and time zone. Look at this article
Employee probation application (Luzhou Laojiao)
Is Zhongyan futures safe and reliable?
NPM reports an error: operation not allowed, MKDIR 'C: \ program files \ node JS \ node_ cache _ cacache’
About CIN, scanf and getline, getchar, CIN Mixed use of getline
Enterprise wechat application authorization / silent login
Trc20 fund collection solution based on thinkphp5 version
How does kubernetes use harbor to pull private images
LGB, XGB, cat, k-fold cross validation
Rembg split mask
npm报错 :operation not permitted, mkdir ‘C: \Program Files \node js \node_ cache _ cacache’