0

In the defination of the source code of response.go it is defined that the Body of type io.ReadCloser but while printing the type of Body by the following code it prints *http.http2gzipReader. Are they both same?

package main

import (
    "fmt"
    "net/http"
)

func main() {
    //any url
    url := "https://www.goal.com/en-in"
    res, _ := http.Get(url)
    body := res.Body
    fmt.Printf("tpye is %T", body)
}

Abishek Bashyal
  • 307
  • 1
  • 6
  • 15
  • 1
    "Are they both same?" No they are not, however `*http.http2gzipReader` implements `io.ReadCloser` and because of that it can be use as such. – mkopriva Apr 06 '20 at 12:48
  • 1
    See https://tour.golang.org/methods/9 and https://golang.org/ref/spec#Interface_types for more info on interfaces. – mkopriva Apr 06 '20 at 12:50
  • @mkopriva why the response.Body type is *http.http2gzipReader instead of io.ReadCloser? Can you share some resourses? – Abishek Bashyal Apr 06 '20 at 13:33
  • It's declared as an interface type so that more than just one concrete implementation can be used. – mkopriva Apr 06 '20 at 13:57

1 Answers1

0

No, they are not same.

io.ReadCloser interface is the type of request body but the *http.http2gzipReader is the concrete type assigned to the interface. That's what we call, *http.http2gzipReader implements io.ReadCloser interface.