且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

learn go defer

更新时间:2022-08-12 21:12:19

package main

// 参考文档:
//     https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.4.md

import "fmt"

func main() {
    function1()
}

func function1() {
    fmt.Printf("In function1 at the top\n")
    defer function2()
    fmt.Printf("In function1 at the buttom\n")
}

func function2() {
    fmt.Printf("function2: Deferred until the end of the calling function!\n")
}