go 的return值在defer前返回,defer延迟执行

package main

import "fmt"

var name string= "go"

func TestName() string {
defer func() {
name="python"
}()
fmt.Println(name)
return name

}

func main() {

myname:=TestName() //go
fmt.Println(name) // python
fmt.Println(myname) // return go






}




//

go
python
go

 

 

 

func Testdefer() (i int){
i = 100

defer func() {
i += 100
//fmt.Println(i)
}()
return i

}

func test(i int) func() (i int ){ //闭包
//
return func() int {
i+=1000
return i
}


}

func main() {
x:=Testdefer()
fmt.Println(x)
f:=test(100)
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())

//

GOROOT=C:\Program Files\Go #gosetup
GOPATH=C:\Users\Administrator\go #gosetup
"C:\Program Files\Go\bin\go.exe" build -o C:\Users\Administrator\AppData\Local\Temp\___1go_build_test17_go.exe D:\awesomeProject\src\test\test17.go #gosetup
C:\Users\Administrator\AppData\Local\Temp\___1go_build_test17_go.exe #gosetup
200
1100
2100
3100

进程 已完成,退出代码为 0













}
上一篇:gorm.Clause()子句分析之ON DUPLICATE KEY UPDATE


下一篇:css样式选择器