Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 将自定义中间件类型传递给alice.New()函数时,生成失败_Go_Go Alice - Fatal编程技术网

Go 将自定义中间件类型传递给alice.New()函数时,生成失败

Go 将自定义中间件类型传递给alice.New()函数时,生成失败,go,go-alice,Go,Go Alice,这是关于我在尝试构建应用程序时遇到的错误 我使用Gorilla mux作为路由器,使用Alice链接中间件 我定义了一个名为“Middleware”的自定义类型,其签名如下: type Middleware func(http.Handler) http.Handler 下面是我使用Alice链接中间件和处理程序的代码 if len(config.Middlewares()) > 0 { subRouter.Handle(config.Path(), alice.New(confi

这是关于我在尝试构建应用程序时遇到的错误

我使用Gorilla mux作为路由器,使用Alice链接中间件

我定义了一个名为“Middleware”的自定义类型,其签名如下:

type Middleware func(http.Handler) http.Handler
下面是我使用Alice链接中间件和处理程序的代码

if len(config.Middlewares()) > 0 {
   subRouter.Handle(config.Path(), alice.New(config.Middlewares()...).Then(config.Handler())).Methods(config.Methods()...).Schemes(config.Schemes()...)
}
但是当我尝试构建时,我在控制台中得到以下错误:

infrastructure/router.go:88:63: cannot use config.Middlewares() (type []Middleware) as type []alice.Constructor in argument to alice.New
我已经检查了alice.Constructor的代码。它还具有与我的中间件类型相同的签名

我正在使用Go 1.13和以下版本的Alice

github.com/justinas/alice v1.2.0

你能帮我整理一下吗?

alice。构造函数具有相同的签名,但定义为另一种类型。所以你不能只用它

看这个 这是一个很好的解释

您可以使用
类型别名
像这样:

var Middleware = alice.Constructor
将如下所示:

之前:


func timeoutHandler(h http.Handler) http.Handler {
    return http.TimeoutHandler(h, 1*time.Second, "timed out")
}

func myApp(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello world!"))
}

type Middleware func(http.Handler) http.Handler

func main() {
    middlewares := []Middleware{timeoutHandler}

    http.Handle("/", alice.New(middlewares...).ThenFunc(myApp))
}

func timeoutHandler(h http.Handler) http.Handler {
    return http.TimeoutHandler(h, 1*time.Second, "timed out")
}

func myApp(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello world!"))
}

type Middleware = alice.Constructor

func main() {
    middlewares := []Middleware{timeoutHandler}

    http.Handle("/", alice.New(middlewares...).ThenFunc(myApp))
}
之后:


func timeoutHandler(h http.Handler) http.Handler {
    return http.TimeoutHandler(h, 1*time.Second, "timed out")
}

func myApp(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello world!"))
}

type Middleware func(http.Handler) http.Handler

func main() {
    middlewares := []Middleware{timeoutHandler}

    http.Handle("/", alice.New(middlewares...).ThenFunc(myApp))
}

func timeoutHandler(h http.Handler) http.Handler {
    return http.TimeoutHandler(h, 1*time.Second, "timed out")
}

func myApp(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello world!"))
}

type Middleware = alice.Constructor

func main() {
    middlewares := []Middleware{timeoutHandler}

    http.Handle("/", alice.New(middlewares...).ThenFunc(myApp))
}

不支持在不同元素类型的片之间进行赋值或转换。使用
alice.Constructor
替换您的类型
Middleware
,或者编写for循环,将元素从
Middleware
切片复制到
alice.Constructor
的新切片。感谢Lucas的澄清和Youtube链接!