我有以下 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}運行時,它會EOF json在迭代獲取請求上引發(fā)錯誤,但是當我單獨點擊 URL 時,它們似乎沒有格式錯誤。我錯過了什么?
在 Go 中請求多個 URL
慕桂英3389331
2021-09-27 16:39:42