Question: i am using [Gorilla Mux](http://www.gorillatoolkit.org/pkg/mux) for writing a REST API and i am having trouble organizing my routes, currently all of my routes are defined in the `main.go` file like this

```
//main.go
package main

import (
    "NovAPI/routes"
    "fmt"
    "github.com/gorilla/mux"
    "net/http"
)

func main() {

    router := mux.NewRouter().StrictSlash(true)

    router.HandleFunc("/hello", func(res http.ResponseWriter, req *http.Request) {
        fmt.Fprintln(res, "Hello")
    })

    router.HandleFunc("/user", func(res http.ResponseWriter, req *http.Request) {
        fmt.Fprintln(res, "User")
    })

    router.HandleFunc("/route2", func(res http.ResponseWriter, req *http.Request) {
        fmt.Fprintln(res, "Route2")
    })

    router.HandleFunc("/route3", func(res http.ResponseWriter, req *http.Request) {
        fmt.Fprintln(res, "Route3")
    })

    // route declarations continue like this

    http.ListenAndServe(":1128", router)

}

```

so what i want to do is take out and split this route declaration into multiple files, how would i go about doing that? **thanks in advance.**

Answer: 
You can modularize your routers independently into different packages, and mount them on the main router

Elaborating just a little on the following [issue](https://github.com/gorilla/mux/issues/136), you can come up with this approach, that makes it quite scalable (and easier to test, to some degree)

/api/router.go

```golang
package api

import (
    "net/http"

    "github.com/gorilla/mux"
)

func Router() *mux.Router {
    router := mux.NewRouter()
    router.HandleFunc("/", home)
    return router
}

func home(w http.ResponseWriter, req *http.Request) {
    w.Write([]byte("hello from API"))
}

```

/main.go

```golang
package main

import (
    "log"
    "net/http"
    "strings"

    "github.com/...yourPath.../api"
    "github.com/...yourPath.../user"
    "github.com/gorilla/mux"
)

func main() {
    router := mux.NewRouter()

    router.HandleFunc("/", home)
    mount(router, "/api", api.Router())
    mount(router, "/user", user.Router())

    log.Fatal(http.ListenAndServe(":8080", router))
}

func mount(r *mux.Router, path string, handler http.Handler) {
    r.PathPrefix(path).Handler(
        http.StripPrefix(
            strings.TrimSuffix(path, "/"),
            handler,
        ),
    )
}

func home(w http.ResponseWriter, req *http.Request) {
    w.Write([]byte("Home"))
}

```
Since I am new to Go, I prefer a less verbose solution. In each module, we can create a Route function that expects a main route pointer and creates sub-routes to it. Our main.go file would be as follows

```
package main

import (
    "net/http"
    "github.com/user-name/repo-name/auth"
    "github.com/gorilla/mux"
)

func main() {
    r := mux.NewRouter()
    auth.Router(r)
    http.ListenAndServe(":8080", r)
}

```

then in auth module, we can create a route file

```
package auth

import "github.com/gorilla/mux"

func Router(r *mux.Router) {
    routes := r.PathPrefix("/auth").Subrouter()

    routes.HandleFunc("/register", Register)
}

```