当前位置:网站首页>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 .

边栏推荐
- 5g new scheme! Upgrade the existing base station and UE simulator to 5g millimeter wave band
- Qiming Zhixian shares the application scheme of 2.8-inch handheld central control screen
- Particle filter learning record
- 3D reconstruction system | L3 dual view motion recovery structure (SFM binocular SFM)
- Servlet连接数据库实现用户登录功能
- jupyter notebook新環境快捷方式
- jupyter notebook新环境快捷方式
- 学习是一件逆人性的事情(成为高手的内功心法)
- Leetcode daily question - fair candy bar exchange
- Qiming cloud sharing | demonstrate the switch through an example of the matter protocol to control the light on and off through the matter protocol
猜你喜欢

Deepin20.6 rtx3080 installing graphics card drivers 510.60.02, cuda11.6, pytorch1.11

Pta: self test -3 array element cyclic right shift problem (20 points)

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

Tcp/ip three handshakes and four waves (interview questions)

5G新方案!升级现有的基站和UE模拟器至5G毫米波频段
![[LDA] LDA theme model notes - mainly Dirichlet](/img/e0/bc96b141aa577106379fab63d9df40.png)
[LDA] LDA theme model notes - mainly Dirichlet

5g new scheme! Upgrade the existing base station and UE simulator to 5g millimeter wave band

Use of boost:: bind() in ROS

Broadcast and multicast (tcp/ip details volume 1/2)

h3c GR5200路由器上如何设置公网ip可以访问
随机推荐
leetcode每日一题-公平的糖果棒交换
简单的爬虫框架:解析51job页面岗位信息
Deepin20.6 rtx3080 installer le lecteur de carte graphique 510.60.02, cuda 11.6, pytorch1.11
Job submission instructions upload jobs to network disk
Explanation of socket principle (where, what and how to use it)
FPGA (III) trigger and latch
Preparation of service for robot moving forward and rotating
UDP总结(TCP/IP详解卷1/2)
Method reference instance method reference
Self induction of exception handling
The process of generating strong association rules from frequent itemsets
vim的安装以及常用命令
广播和多播(TCP/IP详解卷1/2)
Solution of user and root forgetting password in virtual machine
同花顺手机炒股开户安全吗
Acwing summer daily question (sexy prime number on June 10)
Simple crawler framework: parsing 51job page position information
PHP builds a high-performance API architecture based on sw-x framework (II)
Solve log4j2 vulnerability and be attacked by mining and zombie process viruses
How to use grafana to easily realize OVL data visualization