1 回答

TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊
一種方法是這樣,首先添加一個(gè)服務(wù)器類型
type server struct {
router *mux.Router
cities *mongo.Collection
}
將路由包裝器添加到服務(wù)器
func (s *server) routes() {
s.router.HandleFunc("/base", s.handleIndex()).Methods("GET")
}
處理函數(shù)
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 關(guān)注
- 104 瀏覽
添加回答
舉報(bào)