Gorilla Mux
Using Gorilla Mux
go get github.com/gorilla/muxpackage main
import (
"log"
"net/http"
"github.com/gorilla/mux"
)
func home(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case "GET":
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"message": "get called"}`))
case "POST":
w.WriteHeader(http.StatusCreated)
w.Write([]byte(`{"message": "post called"}`))
case "PUT":
w.WriteHeader(http.StatusAccepted)
w.Write([]byte(`{"message": "put called"}`))
case "DELETE":
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"message": "delete called"}`))
default:
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`{"message": "not found"}`))
}
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", home)
log.Fatal(http.ListenAndServe(":8080", r))
}HandleFunc HTTP Methods
Clear is Better than Clever
Subrouter
Path and Query Parameter
Last updated
Was this helpful?