gpt4 book ai didi

go - http和默认servermux之间的区别?

转载 作者:IT老高 更新时间:2023-10-28 13:02:05 30 4
gpt4 key购买 nike

这有什么区别:

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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com