github地址
https://gitee.com/mirrors/mux#examples
参考代码
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
func main() {
// IMPORTANT: you must specify an OPTIONS method matcher for the middleware to set CORS headers
r := mux.NewRouter()
r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
http.Handle("/", r)
http.ListenAndServe(":8080", r)
}
func ArticlesCategoryHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Category: %v
", vars["category"])
}
go run main.php
浏览器访问 http://localhost:8080/articles/werwer/
这个包只是路由分发,http访问还是要自带的http