structServeHTTP405ServeHTTPGet(w http.ResponseWriter, r *http.Request)ServeHTTP
package main
import "net/http"
type Controller struct { }
func notAllowed(w http.ResponseWriter){
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("405- Method Not Allowed"))
}
func(c Controller) Get(w http.ResponseWriter, r *http.Request){
notAllowed(w)
}
func(c Controller) Post(w http.ResponseWriter, r *http.Request){
notAllowed(w)
}
func(c Controller) Put(w http.ResponseWriter, r *http.Request){
notAllowed(w)
}
func(c Controller) Patch(w http.ResponseWriter, r *http.Request){
notAllowed(w)
}
func(c Controller) Delete(w http.ResponseWriter, r *http.Request){
notAllowed(w)
}
func(c Controller) ServeHTTP(w http.ResponseWriter, r *http.Request){
switch r.Method {
case "GET":
c.Get(w, r)
case "POST":
c.Post(w, r)
case "PUT":
c.Put(w, r)
case "PATCH":
c.Patch(w, r)
case "DELETE":
c.Delete(w, r)
}
}
type Index struct {
Controller
}
func(I Index) Get(w http.ResponseWriter, r http.Request){
w.Write([]byte("hello"))
}
func main(){
http.Handle("/", Index{})
http.ListenAndServe(":8080", nil)
}