我正在玩围绕Go的gorilla.mux
库。我有以下配置,但我无法找出到达HelloWorldXml
方法的网址。
func main() {
router := mux.NewRouter()
router.HandleFunc("/{name}.xml", HelloWorldXml).
PathPrefix("/products/")
router.HandleFunc("/hello/{name}", HelloWorld)
http.Handle("/", router)
http.ListenAndServe(":8787",nil)
}
使用的正确网址是什么? http://localhost:8787/products/MyName.xml
返回404。
答案 0 :(得分:10)
func main() {
router := mux.NewRouter()
router.HandleFunc("/{name}.xml", HelloWorldXml)
subrouter := router.PathPrefix("/products/").Subrouter()
//localhost/products/item.xml
subrouter.HandleFunc("/{name}.xml", HelloWorldXmlHandler)
router.HandleFunc("/hello/{name}", HelloWorld)
http.Handle("/", router)
http.ListenAndServe(":8787",nil)
}