首页 > 解决方案 > Go 的 Echo 框架路由器问题?

问题描述

所以这可能是我对 Golang 的有限理解,因为我还在学习它。但是我在我的应用程序的主包/文件中构建了一个工作路由器功能,传入上下文并且工作正常。

但是,当我将它移到它自己的包中时,它停止工作,我来宾这与不从主包中传递上下文有关吗?

所以这有效:

func router(e *echo.Echo) {

  addresController := &controllers.AddressController{
      config.NewController(),
  }

  //Address Routes
  address := e.Group("/address")

  address.GET("/all", func(c echo.Context) error {
     return addresController.AddressList(c)
  })

}

像这样加载到主函数中,

 func main() {
   //Build The Echo Framework
   e := echo.New()

   //Load Router Function
   router(e)

   //Start Echo Web Server
   e.Logger.Fatal(e.Start(":5060"))
 }

但是当我在主函数中使用这个时,

 router.BuidlRoutes(e)

并将所有地址路由放入BuildRoutes函数中,我得到空结果。

谢谢,

更新

BuildRouter要求的功能:

func BuidlRoutes(e *echo.Echo) {
  addresController := &controllers.AddressController{
    config.NewController(),
  }

  //Address Routes
  address := e.Group("/address")

  address.GET("/all", func(c echo.Context) error {
    return addresController.AddressList(c)
  })

  address.GET("/id/:id", func(c echo.Context) error {
    return addresController.AddressByID(c)
  })
}

这是我的Address Controller Struct

type AddressController struct { 
   *config.MyController
}

这个地址控制器正在加载一个 MyController,

type MyController struct {
  *DBSettings
}

func NewController() *MyController {
  return &MyController{}
}

标签: go

解决方案


推荐阅读