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 關注
- 98 瀏覽
添加回答
舉報