package main
import (
"fmt"
"time"
)
/*匿名典型延时场景一:延时执行一段代码*/
func main151() {
defer fmt.Println("我是倒数第一")
defer func(){
defer fmt.Println("我是倒数第二")
defer fmt.Println("我是倒数第三")
defer fmt.Println("我是倒数第四")
}()
fmt.Println("文能复习划重点")
fmt.Println("武能装逼带你飞")
fmt.Println("上九天揽月")
fmt.Println("下五洋捉鳖")
}
/*典型应用场景二:并发执行一段代码*/
func main152() {
//go中的代码跑在【子协程】
go func (){
fmt.Println("春困")
time.Sleep(1*time.Second)
fmt.Println("秋乏")
time.Sleep(1*time.Second)
fmt.Println("夏打瞌睡")
time.Sleep(1*time.Second)
fmt.Println("冬眠")
}()
//以下代码跑在【主协程/
fmt.Println("文能复习划重点")
time.Sleep(1*time.Second)
fmt.Println("武能装逼带你飞")
time.Sleep(1* time.Second)
fmt.Println("上九天揽月")
time.Sleep(1*time.Second)
fmt.Println("下五洋捉鳖")
time.Sleep(2*time.Second)
}
func them(name string,age int) (renpin int) {
fmt.Println(name)
fmt.Println(age)
renpin = 100
return
}
/*带有参数和返回值的匿名函数*/
func main() {
//defer them("张三",20)
defer func(name string,age int) (renpin int) {
fmt.Println(name)
fmt.Println(age)
renpin = 100
return
}("张三",20)
fmt.Println("文能复习划重点")
fmt.Println("武能装逼带你飞")
fmt.Println("上九天揽月")
fmt.Println("下五洋捉鳖")
}
网友评论