尝试包装函数时,将函数用作值时出现错误。

huangapple go评论71阅读模式
英文:

function used as value error while trying to wrap functions

问题

我正在自学使用net/http包,使用《The Way to Go》这本书。他提到了一种将处理函数包装在闭包中的方法,以处理panics,如下所示:

func Index(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Content-Type", "text/html")
    fmt.Fprint(w, "<h2>Index</h2>")
}

func logPanics(function HandleFunc) HandleFunc {
    return func(w http.ResponseWriter, req *http.Request) {
        defer func() {
            if err := recover(); err != nil {
              log.Printf("[%v] caught panic: %v", req.RemoteAddr, err)
            }
        }()
        function(w, req)
    }
}

然后可以像这样调用http.HandleFunc:

http.HandleFunc("/", logPanics(Index))

我想做的是“堆叠”多个函数以包含更多功能。我想添加一个通过.Header().Set(...)添加mime类型的闭包,并且可以像这样调用它:

func addHeader(function HandleFunc) HandleFunc {
    return func(w http.ResponseWriter, req *http.Request) {
        w.Header().Set("Content-Type", "text/html")
        function(w, req)
    }
}
(然后在main())
http.HandleFunc("/", logPanics(addHeader(Index)))

但我认为通过使用一个包装函数来缩短代码会更好,同时保持这些函数的分离:

func HandleWrapper(function HandleFunc) HandleFunc {
    return func(w http.ResponseWriter, req *http.Request) {
        logPanics(addHeader(function(w, req)))
    }
}

但是我得到了一个function(w, req) used as value的错误。我之前没有太多使用闭包的经验,感觉肯定漏掉了什么。

谢谢帮助!

英文:

I'm teaching myself to use the net/http package using The Way to Go book. He mentions a way to wrap handling functions in a closure that takes care of panics like so:

func Index(w http.ResponseWriter, req *http.Request) {
    w.Header().Set(&quot;Content-Type&quot;, &quot;text/html&quot;)
    fmt.Fprint(w, &quot;&lt;h2&gt;Index&lt;/h2&gt;&quot;)
}

func logPanics(function HandleFunc) HandleFunc {
    return func(w http.ResponseWriter, req *http.Request) {
        defer func() {
            if err := recover(); err != nil {
              log.Printf(&quot;[%v] caught panic: %v&quot;, req.RemoteAddr, err)
            }
        }()
        function(w, req)
    }
}

And then call http.HandleFunc using the above like so:

http.HandleFunc(&quot;/&quot;, logPanics(Index))

What I want to do is to "stack" more than one function to include more functionality. I wanted to add a closure that adds a mime type via .Header().Set(...) and I can call it like this:

func addHeader(function HandleFunc) HandleFunc {
    return func(w http.ResponseWriter, req *http.Request) {
        w.Header().Set(&quot;Content-Type&quot;, &quot;text/html&quot;)
        function(w, req)
    }
}
(then in main())
http.HandleFunc(&quot;/&quot;, logPanics(addHeader(Index)))

But I thought it would be nice to shorten that whilst still keeping these functions separate using a wrapper function:

func HandleWrapper(function HandleFunc) HandleFunc {
    return func(w http.ResponseWriter, req *http.Request) {
        logPanics(addHeader(function(w, req)))
    }
}

But I get a function(w, req) used as value error. I haven't
worked much with closures before and I feel like I'm definitely missing something here.

Thanks for the help!

答案1

得分: 2

function(w, req)是一个没有返回值的函数调用,而addHeader期望一个函数作为其参数。

如果你想要将这两个包装函数结合起来,你可能想要像这样做:

func HandleWrapper(function HandleFunc) HandleFunc {
    return logPanics(addHeader(function))
}
英文:

function(w, req) is a function call without a return value, while addHeader expects a function as its argument.

If you want to combine the two wrapper functions, you probably want something like this:

func HandleWrapper(function HandleFunc) HandleFunc {
    return logPanics(addHeader(function))
}

huangapple
  • 本文由 发表于 2013年11月22日 15:53:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/20139746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定