The first program uses the default serve mux. It's identical to the more verbose:
func main() {
http.DefaultServeMux.HandleFunc("/page2", Page2)
http.DefaultServeMux.HandleFunc("/", Index)
http.ListenAndServe(":3000", http.DefaultServeMux)
}
There's one important difference between the two programs: The first program does not have complete control over the handlers used in the program. There are packages that automatically register with the default serve mux from init()
functions (example). If the program imports one of these packages directly or indirectly, the handlers registered by these handlers will be active in the first program.
The second program has complete control over the handlers used with the server. Any handlers registered with the default serve mux are ignored.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…