go-defer用例

package main
import (
	"fmt"
       )
func main() {
	a := 1
	b := 2
	defer func() {
	    err := recover() //recover只能放在defer之后使用
	    fmt.Println(err)
	}() //匿名函数
	swap(a, b)
}
func swap(a, b int) {
	a, b = b, a
	fmt.Println(a, b)
	panic("panic is wrong")
}
posted @ 2020-07-12 00:15  kylingx  阅读(85)  评论(0编辑  收藏  举报