Method Multiplexer for http.ServeMux
package main
import (
"io"
"net/http"
"github.com/acoshift/methodmux"
)
func main() {
mux := http.NewServeMux()
mux.Handle("/", methodmux.Get(http.HandlerFunc(index)))
mux.Handle("/about", methodmux.Mux{
http.MethodGet: http.HandlerFunc(aboutGet),
http.MethodPost: http.HandlerFunc(aboutPost),
"": http.HandlerFunc(aboutOther),
})
http.ListenAndServe(":8080", mux)
}
func index(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello, Method Mux!")
}
func aboutGet(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "About Get")
}
func aboutPost(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "About Post")
}
func aboutOther(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "About does not support method "+r.Method)
}