在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

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.