【问题标题】:Go language cannot use r (type *mux.Router) as type *mux.Route in return argumentGo 语言不能在返回参数中使用 r (type *mux.Router) 作为 type *mux.Route
【发布时间】:2017-05-08 21:00:36
【问题描述】:

嗨,我正在其他 Go 文件中初始化路由器,并在主文件中返回它的引用,然后在主文件中我正在初始化服务。 这段代码没有编译

package router

import (
    "github.com/gorilla/mux"
    "binapp/controllers"
)

const all_bin string = "/bin"

func InitRouter() (*mux.Route) {
    r := mux.NewRouter().StrictSlash(true)

    r.HandleFunc(all_bin, controllers.BinController)

    return r
}


package main

import (
    "binapp/router"
    "log"
    "net/http"
)

func main() {
    router := router.InitRouter()
    log.Fatal(http.ListenAndServe(":9096", router))
}

我收到以下错误:

# binapp/router
binapp/router/Route.go:15: cannot use r (type *mux.Router) as type *mux.Route in return argument

【问题讨论】:

    标签: go servemux


    【解决方案1】:

    InitRouter 的返回类型错误。您返回 *mux.Router,但指定返回类型为 *mux.Route

    【讨论】:

    • Y Y Y 在这个地球上我确实犯了那个错误。我现在把自己锁在浴室里一个小时。
    猜你喜欢
    • 2018-05-10
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 2020-01-12
    • 2011-10-03
    相关资源
    最近更新 更多