当前位置:网站首页>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
边栏推荐
- MySQL小练习(仅适合初学者,非初学者勿进)
- Project upload part
- To remember the composition ~ the pre order traversal of binary tree
- NPM reports an error: operation not allowed, MKDIR 'C: \ program files \ node JS \ node_ cache _ cacache’
- npm报错 :operation not permitted, mkdir ‘C: \Program Files \node js \node_ cache _ cacache’
- 653. 两数之和 IV - 输入 BST
- Wechat: get the owner of a single tag
- Unfortunately, I broke the leader's confidential documents and spit blood to share the code skills of backup files
- 【SQL server速成之路】数据库的视图和游标
- Research purpose, construction goal, construction significance, technological innovation, technological effect
猜你喜欢

Brief steps to build a website / application using flash and H5

valgrind和kcachegrind使用运行分析

501. Mode in binary search tree
![[in-depth good article] detailed explanation of Flink SQL streaming batch integration technology (I)](/img/c9/43a63f526068ef6a3e4964a22c5a1f.png)
[in-depth good article] detailed explanation of Flink SQL streaming batch integration technology (I)

112. 路径总和

机器学习(六)——贝叶斯分类器
![3、 6 [Verilog HDL] gate level modeling of basic knowledge](/img/36/46f2413ecb12f81c003848c93f6bc9.jpg)
3、 6 [Verilog HDL] gate level modeling of basic knowledge

BK3633 规格书

Pctp test experience sharing

Distributed message oriented middleware framework selection - Digital Architecture Design (7)
随机推荐
What is monitoring intelligent playback and how to use intelligent playback to query video recording
SAP 101K 411k inventory change
Wechat applet catchtap = "todetail" event problem
2021 Li Hongyi's adaptive learning rate of machine learning
Flink reads MySQL and PgSQL at the same time, and the program will get stuck without logs
Group Backpack
Image processing in opencv -- Introduction to contour + contour features
Non duplicate data values of two MySQL query tables
To remember the composition ~ the pre order traversal of binary tree
Get trustedinstaller permission
基于ThinkPHP5版本TRC20-资金归集解决方案
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
Common errors of VMware building es8
A must see wechat applet development guide 1 - basic knowledge
[original] use system Text. JSON formats the JSON string
Resource packaging dependency tree
错题汇总1
LGB, XGB, cat, k-fold cross validation
三、6【Verilog HDL】基础知识之门级建模
Data visualization: use Excel to make radar chart