我正在嘗試在我的 go lang 應(yīng)用程序中創(chuàng)建 2 個(gè) HTTP 服務(wù)器,這就是我嘗試實(shí)現(xiàn)它的方法:package mainimport ( "net/http")func main() { server := http.Server{ Addr: ":9000", //Handler: http.HandleFunc("/", hello) } server.ListenAndServe() server2 := http.Server{ Addr: ":8000", //Handler: http.HandleFunc("/", hello) } server2.ListenAndServe()}我遇到的問(wèn)題是,當(dāng)我向?yàn)g覽器發(fā)出請(qǐng)求時(shí),它會(huì)發(fā)出http://localhost:9000/請(qǐng)求,但是當(dāng)我向?yàn)g覽器發(fā)出請(qǐng)求時(shí),http://localhost:8000/我收到“無(wú)法訪問(wèn)站點(diǎn)”。為什么我無(wú)法在 Go 中創(chuàng)建 HTTP 服務(wù)器的實(shí)例?
1 回答

MMTTMM
TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個(gè)贊
ListenAndServe
阻塞一樣,第一個(gè)服務(wù)器啟動(dòng),但隨后不會(huì)繼續(xù)進(jìn)行第二個(gè)調(diào)用。解決這個(gè)問(wèn)題的一個(gè)簡(jiǎn)單方法是從server
它自己的 goroutine 開(kāi)始,比如
func main() {
? ? server := http.Server{
? ? ? ? Addr:? ? ":9000",
? ? ? ? //Handler:? http.HandleFunc("/", hello)
? ? }
? ? go server.ListenAndServe()
? ? server2 := http.Server{
? ? ? ? Addr:? ? ":8000",
? ? ? ? //Handler:? http.HandleFunc("/", hello)
? ? }
? ? server2.ListenAndServe()
}
- 1 回答
- 0 關(guān)注
- 114 瀏覽
添加回答
舉報(bào)
0/150
提交
取消