So, I'm working on parsing a POST using GO. What I want is the body of the post, so I try the following (r
is of type *http.Request
in this context):
var body io.Reader
var d []byte
body = r.Body.Reader
body.Read( d)
However, this results in a compilation error:
Compile error: <file>:44:
r.Body.Reader undefined (type io.ReadCloser has no field or method Reader)
Odd. I could have sworn that it was defined in the docs... Ah! here it is.
Now, I'm fairly new to Go, but this smells a little odd -- what have I screwed up?
From your link, the doc for a ReadCloser
is:
type ReadCloser interface {
Reader
Closer
}
What this is telling you, is that a ReadCloser interface is composed of a Reader
and a Closer
functionality. It IS both. That means the ReadCloser
takes on those interface definitions. They are not actually members, the way you are accessing them.
A Reader
is:
type Reader interface {
Read(p []byte) (n int, err error)
}
So that means you should be accessing Read
like this:
body = r.Body
body.Read(d)
The way interfaces are defined in Go documents, it looked like it was a "has-a" relationship. It's actually an "is-a" relationship, so the following code does what I want:
var d []byte
r.Body.Read(d)