doupiai5597 2017-01-23 16:22
浏览 592

在Iris,Party中将路由器分组为外部文件

I am learning Golang and decided to try out Iris as it is currently the most popular framework, just out of curiosity.

How do I group routes into external files?

main.go

// See apiRoutes here
apiRouter := iris.Party("/api", apiRoutes)

api_routes.go

func apiRoutes() {

    iris.Get("/blog", ...)

    iris.Get("/news", ...)
}

So a call to /api/blog would be processed within api_routes.go inside the iris.Get("/blog") function.

Update: My current project structure is like so. I would also like to continuously nest my routes:

|- Routes
    |- api_routes.go
    |- blog
    |- blog_routes.go
        |- posts
            blog_posts_routes.go
        |- categories
            blog_categories_routes.go
|- main.go
  • 写回答

1条回答 默认 最新

  • dongtan4046 2017-01-23 16:33
    关注

    If you are planning to keep routes in separate file then you have two options

    1. Both main.go and api_routes.go MUST be in same package, probably in your case it should be "package main"
    2. If you are keeping api_routes.go in separate package then you have to import that package in main.go.
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?