277

In my project, I have a byte slice from a request's response.

defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
    log.Println("StatusCode为" + strconv.Itoa(resp.StatusCode))
    return
}

respByte, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("fail to read response data")
    return
}

This works, but if I want to get the response's body for io.Reader, how do I convert? I tried the newreader/writer but wasn't successful.

Nik
  • 2,885
  • 2
  • 25
  • 25
Chan Willson
  • 2,803
  • 2
  • 13
  • 6
  • 3
    If I understand correctly you want the response as `io.Reader`? In that case resp.Body is already of that type. – Arjan Apr 20 '15 at 11:13
  • 3
    http://golang.org/pkg/bytes/#NewReader will "turn" a `[]byte` to an `io.Reader` (and io.ReadSeeker) – ANisus Apr 20 '15 at 11:18
  • @Arjan yap~body...i want get the response as io.reader .but in the base class when i finish the request,the response have been killed.. just save a response body as []byte in struct. – Chan Willson Apr 20 '15 at 11:21
  • @ANisus i will try....thx dude..by the way...it's look's like different type.. – Chan Willson Apr 20 '15 at 11:21
  • @ChanWillson: That requirement makes more sense, the body cannot be read after closing it. As suggested use `*bytes.Reader` which implements the `io.Reader` interface. – Arjan Apr 20 '15 at 11:24
  • @ChanWillson It is a different type. One is an interface, the other a value that implements that interface. I created an answer from my comment, trying to explain it a bit. – ANisus Apr 20 '15 at 12:05

2 Answers2

465

To get a type that implements io.Reader from a []byte slice, you can use bytes.NewReader in the bytes package:

r := bytes.NewReader(byteData)

This will return a value of type bytes.Reader which implements the io.Reader (and io.ReadSeeker) interface.

Don't worry about them not being the same "type". io.Reader is an interface and can be implemented by many different types. To learn a little bit more about interfaces in Go, read Effective Go: Interfaces and Types.

Pang
  • 9,564
  • 146
  • 81
  • 122
ANisus
  • 74,460
  • 29
  • 162
  • 158
  • Is there a similar function to turn a `[]byte` into an `io.Writer`? E.g. `bytes.NewWriter(destination)` – byxor Oct 12 '18 at 19:32
  • 6
    @byxor Yes, what you are looking for is [`bytes.Buffer`](https://golang.org/pkg/bytes/#Buffer). There you create a buffer that implements `io.Writer` like this: `w := bytes.NewBuffer(destination)`. – ANisus Oct 14 '18 at 14:54
  • I do not understand this error from `govet`: `cannot use *bytes.NewReader(out.Bytes()) (type bytes.Reader) as type io.Reader in argument to ioutil.NopCloser` :( – Vitaly Zdanevich Apr 10 '19 at 16:47
  • 1
    @VitalyZdanevich That is because `bytes.Reader` has pointer receivers, and you are turning it from a pointer type (`*bytes.Reader`) to a base type (`bytes.Reader`) . Just get rid of the asterisk (`*`) and you should be fine :) – ANisus Apr 10 '19 at 18:46
0

Create a buffer also works:

r := bytes.NewBuffer(byteData)
xblymmx
  • 39
  • 2
  • 3