登录
首页 >  Golang >  Go问答

恐慌:http:/(根路径)的多个注册

来源:stackoverflow

时间:2024-04-12 15:15:31 414浏览 收藏

有志者,事竟成!如果你在学习Golang,那么本文《恐慌:http:/(根路径)的多个注册》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我尝试在不同的端口上启动两个 http 服务器,但无法使用相同的模式:

handlerFunc1 := http.HandlerFunc(hello1)
http.Handle("/", handlerFunc1)
server1 := &http.Server{
    Addr:    "localhost:8081",
    Handler: handlerFunc1,
}
go server1.ListenAndServe()

http.HandleFunc("/", hello2)
go http.ListenAndServe(":8082", nil)

你知道怎么做吗,我尝试过使用(如你所见)http.serverhttp.listenandserve


正确答案


对于任何其他进步的开发人员来说,这都有效:

mux1 := http.NewServeMux()
mux1.HandleFunc("/", hello1)
go http.ListenAndServe(":8081", mux1)


mux2 := http.NewServeMux()
mux2.HandleFunc("/", hello2)
go http.ListenAndServe(":8082", mux2)

感谢@mkopriva's comment

以上就是《恐慌:http:/(根路径)的多个注册》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>