当前位置:网站首页>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
边栏推荐
- C problem of marking the position of polygons surrounded by multiple rectangles
- 读书笔记
- Analysis of Nacos source code
- 内网渗透系列:内网隧道之icmp_tran
- 面试学习路线
- linux下mysql数据库备份与恢复(全量+增量)
- Houdini > rigid body, rigid body breaking RBD
- 《内网安全攻防:渗透测试实战指南》读书笔记(六):域控制器安全
- Intranet penetration series: ICMP of Intranet tunnel_ Tran
- Research on system and software security (3)
猜你喜欢
![[unity VFX] Introduction notes of VFX special effects - spark production](/img/bb/a6c637d025dfb8877e6b85e7f39d6b.png)
[unity VFX] Introduction notes of VFX special effects - spark production

TA notes of Zhuang understand (VII) < Lambert + Phong + shadow + 3evcolor + Ao >

CTF attack and defense world brush questions 51-

DVWA靶场练习

攻防世界MISC刷题1-50

CTF-MISC学习之从开始到放弃

When using flash, the code ends automatically without an error, the connection cannot be maintained, and the URL cannot be accessed.

Protobuf use

内网渗透系列:内网隧道之dnscat2

Feign源码分析
随机推荐
SAP tr manual import system operation manual
Redis事务实现乐观锁原理
What's new in. Net 5 NET 5
Reptile learning notes, learning reptile, read this article is enough
Three minutes to teach you to use Houdini fluid > > to solve particle fluid droplets
Reading notes
Interview learning route
Internal network security attack and defense: a practical guide to penetration testing (IV): Authority improvement analysis and defense
Internal network security attack and defense: a practical guide to penetration testing (VII): cross domain attack analysis and defense
Intranet security attack and defense: a practical guide to penetration testing (6): domain controller security
如何展示您的数字作品集:来自创意招聘人员的建议
[unity VFX] Introduction notes of VFX special effects - spark production
Link to some good tutorials or notes about network security and record them
SAP Query增强开发介绍
关于unity获取真实地理地图转3D化的相关链接
Cloud computing skills competition -- Part 2 of openstack private cloud environment
Search and replacement of C text file (WinForm)
Ctf-misc learning from start to give up
C smoothprogressbar custom progress bar control
一些关于网络安全的好教程或笔记的链接,记录一下