我只是在 Golang 中接近 webapps。這是作為起點(diǎn)的簡單代碼:package mainimport ( "fmt" "log" "net/http")const ( CONN_HOST = "localhost" CONN_PORT = "8080")func helloWorld(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello World!")}func main() { http.HandleFunc("/", helloWorld) err := http.ListenAndServe(CONN_HOST+":"+CONN_PORT, nil) if err != nil { log.Fatal("error starting http server : ", err) return }}執(zhí)行:go run http-server.gocurl http://localhost:8080/Hello World!但是在網(wǎng)絡(luò)瀏覽器中打開 ip 地址時(shí):http://111.111.1.1:8080/connection didn't succeed如果我替換這段代碼: err := http.ListenAndServe(CONN_HOST+":"+CONN_PORT, nil) if err != nil { log.Fatal("error starting http server : ", err) return }和 : log.Fatal(http.ListenAndServe(":8080", nil))所以 main() 函數(shù)僅由這兩行組成: func main() { http.HandleFunc("/", helloWorld) }curl http://localhost:8080/Hello World!在網(wǎng)絡(luò)瀏覽器中:http://111.111.1.1:8080/Hello World!那么....如何使原始的簡單 http-server.go 在網(wǎng)絡(luò)瀏覽器中工作,而不僅僅是使用命令行 curl ?期待您的幫助。馬可
1 回答

慕姐8265434
TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊
你的服務(wù)器監(jiān)聽的ip地址是localhost
,所以它只處理請求到localhost
。
你可以嘗試curl http://111.111.1.1:8080/
,你也會失敗。
如果你想從局域網(wǎng)或任何其他 IP 訪問你的服務(wù)器,你應(yīng)該設(shè)置CONN_HOST = "111.111.1.1"。
- 1 回答
- 0 關(guān)注
- 125 瀏覽
添加回答
舉報(bào)
0/150
提交
取消