Go语言 为什么相同的代码在不同的行上工作或不工作?

ryoqjall  于 2023-04-09  发布在  Go
关注(0)|答案(1)|浏览(138)
muxMaps := http.NewServeMux()
muxMaps.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {w.Write([]byte("maps page"))})

mux := http.NewServeMux()
mux.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {w.Write([]byte("main page"))})
mux.Handle("/maps/", http.StripPrefix("/maps", muxMaps))
mux.Handle("/maps1/", http.StripPrefix("/maps1", muxMaps))

http.ListenAndServe(":8000", mux)

请求“http://localhost:8000/”返回“主页”
请求“http://localhost:8000/maps 1/”返回“Map页面”
但是请求“http://localhost:8000/maps/”重定向到“http://localhost:8000/”并返回“main page”
好吧,也许顺序很重要。我试着这样做:

mux.Handle("/maps1/", http.StripPrefix("/maps1", muxMaps))
mux.Handle("/maps/", http.StripPrefix("/maps", muxMaps))

结果是不变的。
为什么?“Map”这个词有什么魔力?

相关问题