英文:
body.Read undefined (type *io.ReadCloser has no field or method Read)
问题
我似乎无法解决这个奇怪的错误。这是我的代码:
resp, err := http.Get("example.com/my/text/file.conf")
...
err = parseEvent(eventchan, &resp.Body)
func parseEvent(eventchan chan Event, body *io.ReadCloser) error {
raw := make([]byte, 1024*1024*32, 1024*1024*32)
n, err := body.Read(raw)
我得到了这个奇怪的错误:
./igen.go:91: body.Read undefined (type *io.ReadCloser has no field or method Read)
第91行是上面的 n, err := body.Read(raw)
行。
我错过了什么?Golang.org告诉我ReadCloser实现了Reader接口,而我正试图调用的Read(p []byte) (n int, err error)
方法就是在该接口中定义的。
英文:
I can't seem to solve this weird error. Here's my code:
resp, err := http.Get("example.com/my/text/file.conf")
...
err = parseEvent(eventchan, &resp.Body)
func parseEvent(eventchan chan Event, body *io.ReadCloser) error {
raw := make([]byte, 1024*1024*32, 1024*1024*32)
n, err := body.Read(raw)
And i get this strange error:
> ./igen.go:91: body.Read undefined (type *io.ReadCloser has no field or method Read)
Row 91 is the n, err := body.Read(raw)
line above.
What did I miss? Golang.org tells me ReadCloser implements Reader, which has the Read(p []byte) (n int, err error)
method that I'm trying to call.
答案1
得分: 7
你的参数是 body *io.ReadCloser
,意味着一个指向接口的指针。ReadCloser
是一个接口,具有 Read()
方法。只需将你的函数签名更改为:
func parseEvent(eventchan chan Event, body io.ReadCloser) error
这样就可以了。
英文:
your parameter is body *io.ReadCloser
- meaning a pointer to an interface. ReadCloser
, the interface, has Read()
. Just change your function signature to:
func parseEvent(eventchan chan Event, body io.ReadCloser) error
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论