当前位置:网站首页>Defer learning in golang
Defer learning in golang
2022-06-12 15:33:00 【fish_ study_ csdn】
1:defer Execution order of
Multiple defer When it appears , It's a “ Stack ” The relationship between , That is, first in and then out . In a function , Written in the front defer It'll be better than the one written later defer Late call .
package main
import "fmt"
func main() {
defer func1()
defer func2()
defer func3()
}
func func1() {
fmt.Println("A")
}
func func2() {
fmt.Println("B")
}
func func3() {
fmt.Println("C")
}
Output results : C B A
2: defer And return Whoever first after
package main
import "fmt"
func deferFunc() int {
fmt.Println("defer func called")
return 0
}
func returnFunc() int {
fmt.Println("return func called")
return 0
}
func returnAndDefer() int {
defer deferFunc()
return returnFunc()
}
func main() {
returnAndDefer()
}The execution result is :
return func called
defer func calledThe conclusion is :return The following statement executes ,defer Execute after the following statement
3: The return value of the function initializes
This knowledge point does not belong to defer In itself , But the scene of the call is different from defer There's a connection , So it's sort of defer One of the necessary knowledge points .
Such as : func DeferFunc1(i int) (t int) {}
Where the return value t int, This t It will be initialized to the zero value of the corresponding type at the beginning of the function, and the scope is the whole function

package main
import "fmt"
func DeferFunc1(i int) (t int) {
fmt.Println("t = ", t)
return 2
}
func main() {
DeferFunc11(10)
}
result :t=0
prove , Just declare the return value of the function and the variable name , When the function is initialized, it will be assigned a value of 0, And it can be seen in the scope of the function body .
4: The return value of a famous function meets defer situation
In the absence of defer Under the circumstances , In fact, the return of a function is the same as return coincident , But with defer Not so .
We go through Knowledge point 2 hear , First return, Again defer, So at the end of the execution return after , We have to do it again defer The sentence in , You can still modify the results that should have been returned .
package main
import "fmt"
func returnButDefer() (t int) { //t initialization 0, And the scope is the whole scope of the function
defer func() {
t = t * 10
}()
return 1
}
func main() {
fmt.Println(returnButDefer())
} The returnButDefer() The expected return value is 1, But in return after , Has been defer The anonymous func Function execution , therefore t=t*10 Be performed , Last returnButDefer() Back to the top main() As the result of the 10
5: defer meet panic
We know , Can trigger defer What I want is to meet return( Or function body to the end ) And meet panic.
according to Knowledge point 2, We know ,defer meet return The situation is as follows :

that , encounter panic when , All over the history of the association defer Linked list , And implement defer. In execution defer In the process : encounter recover Then stop panic, return recover Continue to carry on . If I don't meet recover, Go through this process defer After the list , towards stderr Throw out panic Information .

边栏推荐
- Conversion between sparse array and array and file reading and writing
- 增加mysql的最大连接数
- Pta: self test -3 array element cyclic right shift problem (20 points)
- Simple crawler framework: parsing 51job page position information
- [game server design cases] insights
- Apprendre est une chose contre la nature humaine
- Task output: dense snow ice city theme song 0612
- Understanding of Odom coordinate system
- Kinect2.0+ORBSLAM2_with_pointcloud_map
- Module yaml error: Unexpected key in data: static_ context [line 9 col 3]
猜你喜欢

Solve log4j2 vulnerability and be attacked by mining and zombie process viruses

IGMP报文(TCP/IP详解卷1/卷2)

Design concept of ORM framework

IGMP message (tcp/ip details volume 1/ Volume 2)

Remote control of other computers -- detailed tutorial

Scala download and idea installation of scala plug-ins (nanny level tutorial is super detailed)

The difference between TCP and UDP, the three handshakes of TCP and the four waves of TCP

Ngork implements intranet penetration -- free

SOA Architecture

TCP与UDP的区别,以及TCP的三次握手和TCP的四次挥手
随机推荐
PTA:自测-3 数组元素循环右移问题 (20分)
sql server 2008 的导入导出步骤
应势而变,2022年下半场的升级之路
Wild pointer understanding
Classification of annotations
FIRSTVT和LASTVT白话版
任务 水果炸汁机 0611
Solution of user and root forgetting password in virtual machine
任务 输出密雪冰城主题曲 0612
Two implementation methods of generic interface
ROS beginners write the server that the little turtle rotates a certain angle at a certain speed
ARM 64指令小记
Design concept of ORM framework
Deepin20.6 RTX3080 安装显卡驱动510.60.02、CUDA11.6、PyTorch1.11
[jvm learning] local method stack and heap
5g new scheme! Upgrade the existing base station and UE simulator to 5g millimeter wave band
First set and follow set in vernacular
Servlet知识详解(2)
安装rhel 7/8 (红帽)虚拟机(转载)
idea 拉取分支代码