英文:
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("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)
}
}
And then call http.HandleFunc using the above like so:
http.HandleFunc("/", 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("Content-Type", "text/html")
function(w, req)
}
}
(then in main())
http.HandleFunc("/", 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))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论