【发布时间】:2016-04-05 12:30:07
【问题描述】:
我正在使用 Gorilla Mux 编写 REST API,但我在组织路由时遇到了麻烦,目前我的所有路由都在 main.go 文件中定义,如下所示
//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)
}
所以我想要做的是取出这个路由声明并将其拆分为多个文件,我将如何去做呢? 提前致谢。
【问题讨论】: