作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
这有什么区别:
func main() {
http.HandleFunc("/page2", Page2)
http.HandleFunc("/", Index)
http.ListenAndServe(":3000", nil)
}
并使用 golang 服务多路复用器
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/page2", Page2)
mux.HandleFunc("/", Index)
http.ListenAndServe(":3000", mux)
}
最佳答案
第一个程序使用 default serve mux .它与更详细的相同:
func main() {
http.DefaultServeMux.HandleFunc("/page2", Page2)
http.DefaultServeMux.HandleFunc("/", Index)
http.ListenAndServe(":3000", http.DefaultServeMux)
}
这两个程序之间有一个重要的区别:第一个程序不能完全控制程序中使用的处理程序。有些包会自动从 init()
函数 ( example ) 注册到默认的服务多路复用器。如果程序直接或间接导入其中一个包,则这些处理程序注册的处理程序将在第一个程序中处于事件状态。
第二个程序可以完全控制与服务器一起使用的处理程序。使用默认服务多路复用器注册的任何处理程序都将被忽略。
关于go - http和默认servermux之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36921190/
我是一名优秀的程序员,十分优秀!