package main
import "fmt"
import "net/http"
func home(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "What!")
}
func bar(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Bar!")
}
func main() {
http.HandleFunc("/", home)
http.HandleFunc("/foo", bar)
http.ListenAndServe(":5678", nil)
}
如果我访问/foo
,bar
将运行。为什么这会在每个URL请求上得到处理?
如果我访问/
或/any/other/path
,home
将运行。
任何想法为什么发生这种情况?我如何处理404的?
我不明白。这是为什么?如果我拿走根处理程序,404的工作。看起来,当你注册一个'/'处理程序时,它充当通配符路由。 – daryl 2014-09-10 21:22:03
这是一个通配符路由,如果您尝试提供文件,请使用http://golang.org/pkg/net/http/#FileServer – OneOfOne 2014-09-10 21:23:25