当前位置:网站首页>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
边栏推荐
- Technological innovation in government affairs in the construction of Digital Government
- 【SQL server速成之路】数据库的视图和游标
- Applet in wechat and app get current ()
- Wechat: get the owner of a single tag
- Introduction to matlab
- Kettle experiment (III)
- Go language self-study series | golang structure as function parameter
- valgrind和kcachegrind使用运行分析
- Kettle实验 转换案例
- Machine learning (VI) -- Bayesian classifier
猜你喜欢
Download and install bashdb
資源打包關系依賴樹
Kettle实验 (三)
Multi view depth estimation by fusing single view depth probability with multi view geometry
To remember the composition ~ the pre order traversal of binary tree
valgrind和kcachegrind使用運行分析
Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
The K neighbors of each sample are obtained by packet switching
Notes on xctf questions
MySQL小练习(仅适合初学者,非初学者勿进)
随机推荐
小程序报错:Cannot read property 'currentTarget' of undefined
Failed to prepare device for development
A must see wechat applet development guide 1 - basic knowledge
Common errors of VMware building es8
员工试用期转正申请书(泸州老窖)
Applet in wechat and app get current ()
Project upload part
[in-depth good article] detailed explanation of Flink SQL streaming batch integration technology (I)
MySQL小练习(仅适合初学者,非初学者勿进)
Arbre de dépendance de l'emballage des ressources
Correct method of calculating inference time of neural network
Valgrind et kcachegrind utilisent l'analyse d'exécution
SQL used query statements
valgrind和kcachegrind使用運行分析
Download and install bashdb
valgrind和kcachegrind使用运行分析
Brief steps to build a website / application using flash and H5
【原创】使用System.Text.Json对Json字符串进行格式化
Withholding agent
Emuelec compilation summary