1 回答

TA贡献1982条经验 获得超2个赞
一种方法是这样,首先添加一个服务器类型
type server struct {
router *mux.Router
cities *mongo.Collection
}
将路由包装器添加到服务器
func (s *server) routes() {
s.router.HandleFunc("/base", s.handleIndex()).Methods("GET")
}
处理函数
func (s *server) handleIndex() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cities := s.cities.Find(...) // something like that
// write your response, etc
}
}
然后在主
func main() {
sr := &server{
router: mux.NewRouter(),
cities: getMongoDBCollection('cities') // implement this one :) should return a *mongo.Collection...
}
sr.routes()
...
}
- 1 回答
- 0 关注
- 105 浏览
添加回答
举报