Go language pursues simplicity and elegance, so Go language does not support the traditional try...catch...finally exception, because the designers of Go language believe that, Mixing exceptions with control structures can easily clutter your code. Because developers can easily abuse exceptions and even throw an exception for a small error.
In Go language, use multi-value return to return errors. Don't use exceptions to replace errors, let alone control the process. In rare cases, that is, when a real exception is encountered (such as the divisor is 0). Only use the Exception handling introduced in Go: defer, panic, recover.
The usage scenarios of these exceptions can be described simply: Go can throw a panic exception, then capture the exception through recover in defer, and then handle it normally.
Example code:
package main import "fmt" func main(){ defer func(){ // 必须要先声明defer,否则不能捕获到panic异常 fmt.Println("c") if err:=recover();err!=nil{ fmt.Println(err) // 这里的err其实就是panic传入的内容,55 } fmt.Println("d") }() f() } func f(){ fmt.Println("a") panic(55) fmt.Println("b") fmt.Println("f") } 输出结果: a c d exit code 0, process exited normally.
defer
##defer Original English meaning: vi. To postpone; to postpone; to obey vt. To postpone ; To postpone. The idea of defer is similar to the destructor in C, but in the Go language, what is "destructed" is not the object, but the function. Defer is used to add statements that are executed when the function ends. Note that the emphasis here is on adding, not specifying, because unlike the destructor in C which is static, the defer in Go is dynamic.func f() (result int) { defer func() { result++ }() return 0 }The above function returns 0, because the function returns before there is time to add the defer thing. It is also worth mentioning that defer can be performed multiple times, thus forming a defer stack, and the subsequent defer statements will be called first when the function returns.
panic
panic is used to indicate a very serious and unrecoverable error. In the Go language, this is a built-in function that receives a value of type interface{} (that is, any value) as a parameter. The function of panic is just like the exceptions we usually come into contact with. However, Go does not have try...catch, so panic will generally cause the program to hang (unless it is recovered). Therefore, exceptions in the Go language are really exceptions. You can try calling panic to see if the program hangs immediately, and then the call stack will be printed out when Go is running. However, the key point is that even if the function panics when it is executed, the function does not go down. The panic is not immediately transferred upward during runtime, but to defer, and everything in defer is finished. , the panic is passed upward again. So at this time defer is a bit similar to finally in try-catch-finally.recover
#As mentioned above, the panic function does not return immediately, but defers first and then returns. At this time (during defer), if there is a way to capture the panic and prevent the panic from being delivered, then the exception handling mechanism will be perfect. Go language provides the recover built-in function. As mentioned earlier, once panic occurs, the logic will go to defer, then we will wait at defer. Calling the recover function will capture the current panic (if If so), the captured panic will not be passed upwards, and peace will be restored to the world. You can do what you want. However, it should be noted that after recovery, the logic will not be restored to the panic point, and the function will still return after defer. For more go language knowledge, please pay attention to thego language tutorial column on the PHP Chinese website.
The above is the detailed content of Introduction to exception handling methods in Go language. For more information, please follow other related articles on the PHP Chinese website!

go语言有缩进。在go语言中,缩进直接使用gofmt工具格式化即可(gofmt使用tab进行缩进);gofmt工具会以标准样式的缩进和垂直对齐方式对源代码进行格式化,甚至必要情况下注释也会重新格式化。

go语言叫go的原因:想表达这门语言的运行速度、开发速度、学习速度(develop)都像gopher一样快。gopher是一种生活在加拿大的小动物,go的吉祥物就是这个小动物,它的中文名叫做囊地鼠,它们最大的特点就是挖洞速度特别快,当然可能不止是挖洞啦。

是,TiDB采用go语言编写。TiDB是一个分布式NewSQL数据库;它支持水平弹性扩展、ACID事务、标准SQL、MySQL语法和MySQL协议,具有数据强一致的高可用特性。TiDB架构中的PD储存了集群的元信息,如key在哪个TiKV节点;PD还负责集群的负载均衡以及数据分片等。PD通过内嵌etcd来支持数据分布和容错;PD采用go语言编写。

go语言需要编译。Go语言是编译型的静态语言,是一门需要编译才能运行的编程语言,也就说Go语言程序在运行之前需要通过编译器生成二进制机器码(二进制的可执行文件),随后二进制文件才能在目标机器上运行。

go语言能编译。Go语言是编译型的静态语言,是一门需要编译才能运行的编程语言。对Go语言程序进行编译的命令有两种:1、“go build”命令,可以将Go语言程序代码编译成二进制的可执行文件,但该二进制文件需要手动运行;2、“go run”命令,会在编译后直接运行Go语言程序,编译过程中会产生一个临时文件,但不会生成可执行文件。

删除map元素的两种方法:1、使用delete()函数从map中删除指定键值对,语法“delete(map, 键名)”;2、重新创建一个新的map对象,可以清空map中的所有元素,语法“var mapname map[keytype]valuetype”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.