Go 中的中间件模式 - 'used as value' 编译错误
Middleware pattern in Go - 'used as value' compile error
我正在尝试为我的函数制作类似于 http.HandlerFunc 使用的模式的中间件,这通常有效:
func middleware(fn http.HandlerFunc) http.HandlerFunc {
return func (wr http.ResponseWriter, r *http.Request) {
fmt.Println("doing something before")
fn(wr, r)
}
}
func main() {
middleware(func (wr http.ResponseWriter, r *http.Request) {
wr.Write([]byte("..."))
})
}
这行不通:
package main
import (
"fmt"
)
type FN func (arg int)
func myFunc(arg int) {
fmt.Println("Arg:",arg)
}
// Logically here I am returning the function not it's value
// http package does this identically, I don't see the difference
func do(fn FN) FN {
return func (arg int) {
fn(arg)
}
}
func main() {
do(myFunc(3))
}
会不会return编译错误:myFunc(3) used as value
如您所见:
https://golang.org/src/net/http/server.go?s=64103:64150#L2055
第 2065 行:
// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(ResponseWriter, *Request)
这个函数签名也没有 return 这个编译的值。
更新:
这个模式是我试图实现的,现在可以了。
package main
import (
"fmt"
)
type FN func (arg int)
func do(fn FN) FN {
return func (arg int) {
fmt.Println("executed do with arg",arg)
// some other code ...
fn(arg) // call wrapped function
}
}
func something(arg int){
fmt.Println("Executed something with arg",arg)
}
func main() {
do(something)(3)
}
输出:
executed do with arg 3
Executed something with arg 3
Program exited.
您正在使用 myFunc(arg int)
- 它没有 return 任何东西,并试图将它的 return 值传递给 do
。看起来你可能想做:
func main() {
do(myFunc)(3)
}
但不确定
我正在尝试为我的函数制作类似于 http.HandlerFunc 使用的模式的中间件,这通常有效:
func middleware(fn http.HandlerFunc) http.HandlerFunc {
return func (wr http.ResponseWriter, r *http.Request) {
fmt.Println("doing something before")
fn(wr, r)
}
}
func main() {
middleware(func (wr http.ResponseWriter, r *http.Request) {
wr.Write([]byte("..."))
})
}
这行不通:
package main
import (
"fmt"
)
type FN func (arg int)
func myFunc(arg int) {
fmt.Println("Arg:",arg)
}
// Logically here I am returning the function not it's value
// http package does this identically, I don't see the difference
func do(fn FN) FN {
return func (arg int) {
fn(arg)
}
}
func main() {
do(myFunc(3))
}
会不会return编译错误:myFunc(3) used as value
如您所见: https://golang.org/src/net/http/server.go?s=64103:64150#L2055
第 2065 行:
// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(ResponseWriter, *Request)
这个函数签名也没有 return 这个编译的值。
更新:
这个模式是我试图实现的,现在可以了。
package main
import (
"fmt"
)
type FN func (arg int)
func do(fn FN) FN {
return func (arg int) {
fmt.Println("executed do with arg",arg)
// some other code ...
fn(arg) // call wrapped function
}
}
func something(arg int){
fmt.Println("Executed something with arg",arg)
}
func main() {
do(something)(3)
}
输出:
executed do with arg 3
Executed something with arg 3
Program exited.
您正在使用 myFunc(arg int)
- 它没有 return 任何东西,并试图将它的 return 值传递给 do
。看起来你可能想做:
func main() {
do(myFunc)(3)
}
但不确定