0
我試圖多次查詢一個網站,但我得到:夠程給出錯誤
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x38 pc=0x400cca]
package main
import (
"fmt"
"net/http"
)
var urls = []string{
"http://site-centos-64:8080/examples/abc1.jsp",
}
type HttpResponse struct {
url string
response *http.Response
err error
}
var ch = make(chan *HttpResponse, 1000) // buffered
func abc(i int){
for _, url := range urls {
resp, err := http.Get(url)
resp.Body.Close()
ch <- &HttpResponse{url, resp, err}
}
}
func asyncHttpGets(urls []string) []*HttpResponse {
responses := []*HttpResponse{}
for i:=0;i<1000;i++{
go abc(i)
}
for {
select {
case r := <-ch:
responses = append(responses, r)
if len(responses) == 1000 {
return responses
}
}
}
return responses
}
func main() {
results := asyncHttpGets(urls)
for _, result := range results {
fmt.Printf("%s status: %s\n", result.url,
result.response.Status)
}
}
作品精絕當我500次做到這一點。