当前位置:网站首页>Confusing defer, return, named return value, anonymous return value in golang
Confusing defer, return, named return value, anonymous return value in golang
2022-07-24 08:27:00 【micro_ cloud_ fly】
demo01
package main
import "fmt"
func main() {
fmt.Println(" Start ")
fmt.Println(1)
fmt.Println(2)
fmt.Println(3)
fmt.Println(" end ")
}
Print :
Start
1
2
3
end
demo02
package main
import "fmt"
func main() {
fmt.Println(" Start ")
defer fmt.Println(1)
defer fmt.Println(2)
defer fmt.Println(3)
fmt.Println(" end ")
}
Print :
Start
end
3
2
1
demo03
package main
import "fmt"
func main() {
fmt.Println(" Start ")
fmt.Println("f1 Return results :",f1())
fmt.Println(" end ")
}
func f1() int {
var a int
defer func() {
a++
}()
return a
}
Start
f1 Return results : 0
end
demo04
package main
import "fmt"
func main() {
fmt.Println(" Start ")
fmt.Println("f1 Return results :",f1())
fmt.Println(" end ")
}
func f1() (a int) {
defer func() {
a++
}()
return a
}
Start
f1 Return results : 1
end
demo05
package main
import "fmt"
func main() {
fmt.Println(" Start ")
fmt.Println("f1 Return results :", f1())
fmt.Println(" end ")
}
func f1() ( a int) {
defer func() {
a++
}()
return 5
}
Start
f1 Return results : 6
end
demo06
package main
import "fmt"
func main() {
fmt.Println(" Start ")
fmt.Println("f1 Return results :", f1())
fmt.Println(" end ")
}
func f1() ( y int) {
x:=5
defer func() {
x++
}()
return x
}
Start
f1 Return results : 5
end
demo07
package main
import "fmt"
func main() {
fmt.Println(" Start ")
fmt.Println("f1 Return results :", f1())
fmt.Println(" end ")
}
func f1() ( x int) {
defer func(x int) {
x++
}(x)
return 5
}
Start
f1 Return results : 5
end
demo08
package main
import (
"fmt"
)
func main() {
fmt.Println("test Execution results :", test(test1))
}
func test(fn func() error) int {
a := 1000
fmt.Println("a:", a)
defer func() {
if p := recover(); p != nil {
a = 2
fmt.Println("error occur")
}
}()
_ = fn()
fmt.Println("999999")
return a
}
func test1() error {
panic("ha")
return nil
}
a: 1000
error occur
test Execution results : 0
demo09
package main
import "fmt"
/** * @author micro.cloud.fly * @date 2022/3/4 11:39 In the morning * @desc */
func main() {
x := 1
y := 2
defer fmt.Println(calc("AA", x, calc("a", x, y)))
x = 10
defer fmt.Println(calc("BB", x, calc("b", x, y)))
y = 20
}
func calc(flag string, x, y int) int {
fmt.Println(flag, x, y, x+y)
return x + y
}
defer At registration time , All parameters must be determined , For details, see https://www.bilibili.com/video/BV1x5411h7Md?p=28
a 1 2 3
AA 1 3 4
b 10 2 12
BB 10 12 22
22
4

边栏推荐
- 栈/堆/队列刷题(下)
- Web3 traffic aggregation platform starfish OS interprets the "p2e" ecosystem of real business
- how to add square on screenshot
- Uva572 oil deposits problem solution
- [Game Collection] mobile phones are about to burst, and a collection of six high-quality pyGame games is coming ~ (source code attached)
- Bit.store, which has attracted much attention, is at a glance of the latest developments
- Arrays and linked lists
- Is it safe to open an account online in Beijing
- Shanghai issued a document to support the entry of NFT cultural exchange: the trend of digital collections has arrived!
- 「题解」蝙蝠侠的麻烦
猜你喜欢

"Problem solution" with score

Will Plato become the risk target of the meta universe? Platofarm has great opportunities

T-SQL query statement

Web3 traffic aggregation platform starfish OS interprets the "p2e" ecosystem of real business

About the big hole of wechat applet promise

Play to earn: a new and more promising game paradigm in the future
![[wechat applet development] (I) development environment and applet official account application](/img/94/b93d5fb6d9e3515a1f218cc4ec6eef.png)
[wechat applet development] (I) development environment and applet official account application

Wei Xiaoli's "pursuer" is coming

Database | simple hospital patient appointment system based on opengauss

Aquanee: the true meaning of "p2e"
随机推荐
MySQL index filesort
Go: Gin basicauth Middleware
Figure storage geabase
How difficult is it to build a digital collection platform?
Kotlin coroutine (II): scope and cancellation
Upload and insert the execle table into the database based on ThinkPHP
国产“火箭心”人工心脏上市 不同人工心脏有什么区别?
Is gamefi in decline or in the future?
JMX console unauthorized access vulnerability
[MySQL] 08: aggregate function
[interview] Why do you need foreach with the for cycle?
[Google play access] payment server token acquisition
Advantages and disadvantages of redis and ZK implementing distributed locks
Enterprises love hybrid app development, and applet container technology can improve efficiency by 100%
[ByteDance] ByteDance access (including login and payment)
[wechat applet development] (III) homepage banner component uses swiper
Figure New Earth: how to import CAD files with modified elevation datum (ellipsoid)
Poj3278 catch the cow
MySQL日期格式化
"Solution" friend of Vulcan