我的本地主機(jī)服務(wù)器正在運(yùn)行,它只是 docker 中的容器進(jìn)程。我正在嘗試實(shí)現(xiàn) Go CLIENT 來(lái)構(gòu)建用于創(chuàng)建、列出、更新、刪除功能的 REST api。當(dāng)我嘗試點(diǎn)擊 URL 時(shí),程序成功退出但給我一個(gè)空響應(yīng)。我進(jìn)一步觀察到,響應(yīng)類型被“分塊”,內(nèi)容長(zhǎng)度為 -1。我是 Go 的新手,并試圖找出可能的原因是什么,或者任何人都可以為這個(gè)問(wèn)題提供解決方案。這是我的代碼 - { package mainimport ( "encoding/json" "fmt" "io/ioutil" "net/http")type Payload struct { Stuff Data}type Data struct { Id string Links Links_container Actions Actions_container AccountID string AgentID string AllocationState string Compute string Created string}type Links_container map[string]stringtype Actions_container map[string]stringfunc main() { url := "http://localhost:8080/v1/containers" res, err := http.Get(url) if err != nil { fmt.Println(err) } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) if err != nil { fmt.Println(err) } var p Payload err = json.Unmarshal(body, &p) if err != nil { panic(err) } fmt.Println(p.Stuff.AccountID, "\n", p.Stuff.Actions, "\n", p.Stuff.AgentID, "\n", p.Stuff.AllocationState, "\n", p.Stuff.Compute, "\n", p.Stuff.Created, "\n", p.Stuff.Id, "\n", p.Stuff.Links)}}
使用 Go 向本地主機(jī)服務(wù)器發(fā)出 GET http 請(qǐng)求時(shí)出現(xiàn)空響應(yīng)
千萬(wàn)里不及你
2021-08-30 15:16:45