登录
首页 >  Golang >  Go问答

为什么这个函数的参数未被正确传递?

来源:stackoverflow

时间:2024-02-20 19:27:25 462浏览 收藏

大家好,今天本人给大家带来文章《为什么这个函数的参数未被正确传递?》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!

问题内容

以下是我当前阅读的 packt 材料“hands-on restful web services with go”中的完整示例。

func filtercontenttype(handler http.handler) http.handler {
    return http.handlerfunc(func(w http.responsewriter, r *http.request) {
        log.println("currently in the check content type middleware")
        // filtering requests by mime type
        if r.header.get("content-type") != "application/json" {
            w.writeheader(http.statusunsupportedmediatype)
            w.write([]byte("415 - unsupported media type. please send json"))
            return
        }
        handler.servehttp(w, r)
    })
}

func setservertimecookie(handler http.handler) http.handler {
    return http.handlerfunc(func(w http.responsewriter, r *http.request) {
        // setting cookie to every api response
        cookie := http.cookie{name: "servertimeutc", value: strconv.formatint(time.now().unix(), 10)}
        http.setcookie(w, &cookie)
        log.println("currently in the set server time middleware")
        handler.servehttp(w, r)
    })
}

func handle(w http.responsewriter, r *http.request) {
    // check if method is post
    if r.method == "post" {
        var tempcity city
        decoder := json.newdecoder(r.body)
        err := decoder.decode(&tempcity)
        if err != nil {
            panic(err)
        }
        defer r.body.close()
        // your resource creation logic goes here. for now it is plain print to console
        log.printf("got %s city with area of %d sq miles!\n", tempcity.name, tempcity.area)
        // tell everything is fine
        w.writeheader(http.statusok)
        w.write([]byte("201 - created"))
    } else {
        // say method not allowed
        w.writeheader(http.statusmethodnotallowed)
        w.write([]byte("405 - method not allowed"))
    }
}

func main() {
    originalhandler := http.handlerfunc(handle)
    http.handle("/city", filtercontenttype(setservertimecookie(originalhandler)))  // !
    http.listenandserve(":8000", nil)
}

该程序仅由 main 函数和其他 3 个函数组成,它们的逻辑是任意的,只是从我书中的示例中复制的。

在底部,我用“!”进行注释,filtercontenttype 使用的参数本身就是一个函数 (setservertimecookie),并且看起来它是用 originalhandler 作为其参数来调用的。

但是当运行这段代码时,执行顺序是:

  1. filtercontenttype 2. setservertimecookie 3. originalhandler

这与我对使用函数作为参数的理解是违反直觉的。我假设 setservertimecookie 将是第一个执行的,但事实并非如此;它的行为就像一个未调用的函数。

这引出了我的问题,是什么导致 setservertimecookie 推迟其执行,尽管语法表明它被作为 filtercontenttype 的参数调用?

我试图简化事情以方便我自己的理解:

func main() {

    one(two(three))
}

func one(f func()) {
    fmt.Println("ONE\n")
    f()
}

func two(f func()) {
    fmt.Println("TWO\n")
    f()
}

func three(){
    fmt.Println("THREE\n")
}

这段代码无法构建,我留下了错误: two(三) 用作值 - 这告诉我 two 正在被调用,与书中的示例不同。

有什么区别,为什么书中的示例不首先调用 setservertimecookie ?我唯一的假设是它与 http.handlerfunc 的实现有关,所以也许我应该从那里开始。

任何能够加快我的理解的见解将不胜感激。


正确答案


这个doesn't compile因为two(三)没有返回值。

我假设您想在这种情况下返回一个函数闭包,因此要修复:

func two(f func()) func() {
    return func() {
        fmt.println("two\n")
        f()
    }
}

https://go.dev/play/p/vBrAO6nwy4X

回到您关于 setservertimecookie 的问题及其对 return http.handlerfunc(fn) 的使用。查看 http.HandlerFunc 的源代码会发现它实际上是一个类型定义 - 而不是传统的函数调用。恕我直言,它实际上是 go 标准库中最强大且被低估的四行代码:

type HandlerFunc func(ResponseWriter, *Request)

// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
    f(w, r)
}

通过创建 http.handlerfunc 的此值,它隐式是 http.handler,因为它提供了 servehttp 方法。因此,这允许根据请求调用此方法 - 这正是 web 服务的设计目的:调用处理程序时将调用底层函数 f

因为在表达式 one(two( three)) 中,函数 two 不作为函数引用传递。相反,使用参数 two 调用函数 two,这不是函数 one 所期望的

今天关于《为什么这个函数的参数未被正确传递?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

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