我有以下 Go 程序:https : //play.golang.org/p/-TUtJ7DIhipackage mainimport ( "encoding/json" "fmt" "io/ioutil" "net/http" "strconv")func main() { body, err := get("https://hacker-news.firebaseio.com/v0/topstories.json") if err != nil { panic(err) } var ids [500]int if err = json.Unmarshal(body, &ids); err != nil { panic(err) } var contents []byte for _, value := range ids[0:10] { body, err := get("https://hacker-news.firebaseio.com/v0/item/" + strconv.Itoa(value) + ".json") if err != nil { fmt.Println(err) } else { contents = append(contents, body...) } } fmt.Println(contents)}func get(url string) ([]byte, error) { res, err := http.Get(url) if err != nil { return nil, err } body, err := ioutil.ReadAll(res.Body) res.Body.Close() return body, err}運(yùn)行時(shí),它會(huì)EOF json在迭代獲取請(qǐng)求上引發(fā)錯(cuò)誤,但是當(dāng)我單獨(dú)點(diǎn)擊 URL 時(shí),它們似乎沒(méi)有格式錯(cuò)誤。我錯(cuò)過(guò)了什么?
在 Go 中請(qǐng)求多個(gè) URL
慕桂英3389331
2021-09-27 16:39:42