当前位置:网站首页>Go语学习笔记 - 异常处理 | 从零开始Go语言
Go语学习笔记 - 异常处理 | 从零开始Go语言
2022-04-23 06:33:00 【剑客阿良_ALiang】
目录
学习笔记,写到哪是哪。
Go语言有内置的错误接口,可以通过实现接口的方式定义异常,和其他语言的try···catch语法不一样。我现在看着特别别扭。
内置的error接口如下:
type error interface { Error() string }
1、使用实现Error接口方式
样例代码如下
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)
}
}
执行结果
Error -> code=1,msg=a <= b
注意
1、构建自定义的异常结构体CustomError,实现Error方法。
2、使用errors的方法构建异常对象
样例代码如下
package main
import (
"errors"
"fmt"
)
var errCust = errors.New("自定义异常")
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)
}
}
执行结果
test_cust error is 自定义异常
小结
感觉还是漏了一些东西,我后面要好好想想学习路线问题,需要停下来反思一下。
版权声明
本文为[剑客阿良_ALiang]所创,转载请带上原文链接,感谢
https://huyi-aliang.blog.csdn.net/article/details/124338137
边栏推荐
- 内网渗透系列:内网隧道之dns2tcp
- FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning
- Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
- CTF attack and defense world brush questions 51-
- Expression related to month, year and day in SVG
- C read INI file and write data to INI file
- Houdini > variable building roads, learning process notes
- Ctf-misc summary
- 内网渗透系列:内网隧道之pingtunnel
- 数据库之MySQL——基础篇
猜你喜欢
《内网安全攻防:渗透测试实战指南》读书笔记(八):权限维持分析及防御
Houdini > variable building roads, learning process notes
Unity C single case mode learning review notes
Buctf MISC brossage
利用sqlmap注入获取网址管理员账号密码
Analysis of Nacos source code
第五章 投资性房地产
The projection vector of a vector to a plane
When using flash, the code ends automatically without an error, the connection cannot be maintained, and the URL cannot be accessed.
Teach-Repeat-Replan: A Complete and Robust System for Aggressive Flight in Complex Environments
随机推荐
CTF-MISC学习之从开始到放弃
Search and replacement of C text file (WinForm)
Dvwa 靶场练习记录
随笔(不定时更新)
Intranet penetration series: icmptunnel of Intranet tunnel (Master James Barlow's)
FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning
sentinel集成nacos动态更新数据原理
About USB flash drive data prompt raw, need to format, data recovery notes
内网渗透系列:内网隧道之icmp_tran
Guoji Beisheng openstack container cloud environment construction
Houdini>建筑道路可变,学习过程笔记
《内网安全攻防:渗透测试实战指南》读书笔记(四):权限提升分析及防御
第五章 投资性房地产
云计算技能大赛 -- openstack私有云环境 第二部分
Houdini terrain and fluid solution (simulated debris flow)
Protobuf use
Houdini > variable building roads, learning process notes
Alibaba sentinel学习QA
Internal network security attack and defense: a practical guide to penetration testing (IV): Authority improvement analysis and defense
Automatically fit single line text into the target rectangle