25

go中defer的一个隐藏功能

 4 years ago
source link: https://studygolang.com/articles/25224
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在开始使用Go进行编码时,Defer是要关注的一个很重要的特性。它非常简单:在任何函数中,给其他函数的调用加上前缀 defer 以确保该函数在外部函数退出之前立即执行,即使外部函数出现异常被中断,该延迟函数也将运行。

但是,你还可以使用defer在任何函数开始后和结束前执行配对的代码。这个隐藏的功能在网上的教程和书籍中很少提到。要使用此功能,需要创建一个函数并使它本身返回另一个函数,返回的函数将作为真正的延迟函数。在 defer 语句调用父函数后在其上添加额外的括号来延迟执行返回的子函数如下所示:

func main() {
    defer greet()() 
    fmt.Println("Some code here...")
}

func greet() func() {
    fmt.Println("Hello!")
    return func() { fmt.Println("Bye!") } // this will be deferred
}

输出以下内容:

Hello!
Some code here...
Bye!

父函数返回的函数将是实际的延迟函数。父函数中的其他代码将在函数开始时(由 defer 语句放置的位置决定)立即执行。

这为开发者提供了什么能力?因为在函数内定义的匿名函数可以访问完整的词法环境(lexical environment),这意味着在函数中定义的内部函数可以引用该函数的变量。在下一个示例中看到的,参数变量在 measure 函数第一次执行和其延迟执行的子函数内都能访问到:

func main() {
    example()
    otherExample()
}

func example(){
    defer measure("example")()
    fmt.Println("Some code here")
}

func otherExample(){
    defer measure("otherExample")()
    fmt.Println("Some other code here")
}

func measure(name string) func() {
    start := time.Now()
    fmt.Printf("Starting function %s\n", name)
    return func(){ fmt.Printf("Exiting function %s after %s\n", name, time.Since(start)) }
}

输出以下内容:

Starting example
Some code here
Exiting example after 0s
Starting otherExample
Some other code here
Exiting otherExample after 0s

此外函数命名的返回值也是函数内的局部变量,所以上面例子中的 measure 函数如果接收命名返回值作为参数的话,那么命名返回值在延迟执行的函数中访问到,这样就能将 measure 函数改造成记录入参和返回值的工具函数。

下面的示例是引用《go 语言程序设计》中的代码段:

func bigSlowOperation() {
    defer trace("bigSlowOperation")() // don't forget the extra parentheses
    // ...lots of work…
    time.Sleep(10 * time.Second) // simulate slow
    operation by sleeping
}
func trace(msg string) func() {
    start := time.Now()
    log.Printf("enter %s", msg)
    return func() { 
        log.Printf("exit %s (%s)", msg,time.Since(start)) 
    }
}

可以想象,将代码延迟在函数的入口和出口使用是非常有用的功能,尤其是在调试代码的时候。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK