Goroutine出现错误。

huangapple go评论84阅读模式
英文:

Goroutine gives error

问题

我正在尝试多次轮询一个网站,但是出现了以下错误:

panic: 运行时错误:无效的内存地址或空指针解引用

[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 状态: %s\n", result.url,
			result.response.Status)
	}
}

当我将其执行500次时,完全正常。

英文:

I am trying to poll a website multiple times but I get:

> panic: runtime error: invalid memory address or nil pointer dereference
>
> [signal 0xb code=0x1 addr=0x38 pc=0x400cca]

package main

import (
	&quot;fmt&quot;
	&quot;net/http&quot;
)

var urls = []string{
	&quot;http://site-centos-64:8080/examples/abc1.jsp&quot;,
}

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 &lt;- &amp;HttpResponse{url, resp, err}
		}
}
func asyncHttpGets(urls []string) []*HttpResponse {
	responses := []*HttpResponse{}
	for i:=0;i&lt;1000;i++{
		go abc(i)
	}
	for {
		select {
		case r := &lt;-ch:
			responses = append(responses, r)
			if len(responses) == 1000 {
				return responses
			}
		
		}
	}

	return responses

}

func main() {
	results := asyncHttpGets(urls)
	for _, result := range results {
		fmt.Printf(&quot;%s status: %s\n&quot;, result.url,
			result.response.Status)
	}
}

Works absolutely fine when I do it for 500 times.

答案1

得分: 1

go的stacktrace包含了导致panic的代码行。可能有很多goroutine在运行,但你可以使用grep命令查找你的文件名。
我猜你的服务器有一些连接问题。

在你的abc函数中正确处理err应该能提供更多信息。
如果发生错误,不要调用resp.Body.Close()。在你的main函数中,result.response.Status也不会起作用。

英文:

The go stacktrace contains which line of your code caused the panic. There are probably a lot of goroutines running but you can grep for your filename.
I'd assume that you have some connection issues with your server.

Proper handling of err in your abc function should tell you more.
In case of an error, don't call resp.Body.Close(). And in your main function result.response.Status won't work either.

huangapple
  • 本文由 发表于 2014年1月30日 16:56:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/21451719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定