używam Go wbudowany serwer HTTP i pat odpowiedzieć na niektórych adresów URL:Przekazywanie parametrów do http.HandlerFunc
mux.Get("/products", http.HandlerFunc(index))
func index(w http.ResponseWriter, r *http.Request) {
// Do something.
}
muszę przechodzić w dodatkowy parametr do tej funkcji obsługi - interfejs.
func (api Api) Attach(resourceManager interface{}, route string) {
// Apply typical REST actions to Mux.
// ie: Product - to /products
mux.Get(route, http.HandlerFunc(index(resourceManager)))
// ie: Product ID: 1 - to /products/1
mux.Get(route+"/:id", http.HandlerFunc(show(resourceManager)))
}
func index(w http.ResponseWriter, r *http.Request, resourceManager interface{}) {
managerType := string(reflect.TypeOf(resourceManager).String())
w.Write([]byte(fmt.Sprintf("%v", managerType)))
}
func show(w http.ResponseWriter, r *http.Request, resourceManager interface{}) {
managerType := string(reflect.TypeOf(resourceManager).String())
w.Write([]byte(fmt.Sprintf("%v", managerType)))
}
Jak mogę wysłać na dodatkowym paramter do funkcji obsługi?
Wielkie umysły myślą podobnie! Usuwam moją odpowiedź. :) – sergserg
Hehe, tak, widziałem, że masz __exact__ sam kod w swoim własnym wniosku. – ANisus
Alternatywny podpis: 'indeks func (interfejs rm {}) http.HandlerFunc {return func (w http.ResponseWriter, r * http.Request) {...}}' – elithrar