body.Read未定义(类型*io.ReadCloser没有Read字段或方法)

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

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

huangapple
  • 本文由 发表于 2014年10月17日 05:10:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/26413943.html
匿名

发表评论

匿名网友

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

确定