登录
首页 >  Golang >  Go问答

延迟执行函数

来源:stackoverflow

时间:2024-03-01 11:51:22 218浏览 收藏

有志者,事竟成!如果你在学习Golang,那么本文《延迟执行函数》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我正在学习golang源代码并陷入延迟函数执行顺序。 我有两个文件:一个定义端点的行为,另一个用于测试。我删除了一些与我的问题无关的代码,以减少需要阅读的行数。 端点定义文件

// endpoint is the fundamental building block of servers and clients.
// it represents a single rpc method.
type endpoint func(ctx context.context, request interface{}) (response interface{}, err error)

// middleware is a chainable behavior modifier for endpoints.
type middleware func(endpoint) endpoint

// chain is a helper function for composing middlewares. requests will
// traverse them in the order they're declared. that is, the first middleware
// is treated as the outermost middleware.
func chain(outer middleware, others ...middleware) middleware {
    return func(next endpoint) endpoint {
        for i := len(others) - 1; i >= 0; i-- { // reverse
            next = others[i](next)
        }
        return outer(next)
    }
}

测试文件包含打印的步骤。

func examplechain() {
    e := endpoint.chain(
        annotate("first"),
        annotate("second"),
        annotate("third"),
    )(myendpoint)

    if _, err := e(ctx, req); err != nil {
        panic(err)
    }

    // output:
    // first pre
    // second pre
    // third pre
    // my endpoint!
    // third post
    // second post
    // first post
}

var (
    ctx = context.background()
    req = struct{}{}
)

func annotate(s string) endpoint.middleware {
    return func(next endpoint.endpoint) endpoint.endpoint {
        return func(ctx context.context, request interface{}) (interface{}, error) {
            fmt.println(s, "pre")
            defer fmt.println(s, "post")
            return next(ctx, request)
        }
    }
}

func myendpoint(context.context, interface{}) (interface{}, error) {
    fmt.println("my endpoint!")
    return struct{}{}, nil
}

据我了解,应该首先执行 annotate 三个方法,然后执行 endpoint.chain 方法,最后执行 myendpoint 方法。另外,由于首先打印 pre ,并且当函数返回“post”时,应根据 go 文档中的 defer 解释进行遵循: “defer”语句调用一个函数,该函数的执行被推迟到周围函数返回的那一刻,要么是因为周围函数执行了 return 语句,到达了其函数体的末尾,要么是因为相应的 goroutine 出现了恐慌。

所以我期望看到的是

// Output:
    // first pre
    // first post
    // second pre
    // second post
    // third pre
    // third post
    // my endpoint!

简而言之,我的问题是:

  1. 为什么 first pre 后面没有 first post,与 second third 相同。
  2. posts 的顺序相反。 endpoint.chain 反转 annotate 返回值列表的执行,但首先评估 annotate 方法,对吗?更不用说,打印了 pres,这意味着首先执行内部函数

正确答案


延迟函数作为函数中的最后一件事运行,位于 return 语句之后,因此 annotate 函数将首先运行 next,只有在返回后延迟函数才会运行。根据您的代码,它应该打印的顺序是:

first pre
second pre
third pre
my endpoint
third post
second post
first post

Here is your example turned into something that runs on the Go playground.

请注意,如果您在给定函数中调用 defer 多次,则每个延迟调用均按 lifo 顺序运行。因此,如果您想使用 defer 确保您的 post 首先被调用,然后 next 运行,请考虑替换:

defer fmt.println(s, "post")
next(ctx, request)

与:

defer next(ctx, request)
defer fmt.println(s, "post)

当然,在您的情况下,您希望返回 next 返回的内容,这会产生一个小问题。要在实际情况下解决这个问题,您需要一个小函数和一些命名的返回值:

defer func() { i, e = next(ctx, request) }()

其中 ie 是指定的返回值。

Here is that same code turned into a new example in which the deferred calls occur in the desired order. 在这种情况下,这个例子相当愚蠢,因为没有任何恐慌,并且中间没有“危险步骤”,所以我们真正需要的是顺序执行两个 fmt.println 调用,而不使用 defer 。但是,如果我们可以在 fmt.println(s, "pre") 和 post 部分之间恐慌,那么这可能是有意义的。

好了,本文到此结束,带大家了解了《延迟执行函数》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>