dousuie2222 2017-07-20 12:32
浏览 8
已采纳

Golang类型系统不一致(http软件包)

I am trying to wrap my head around the GoLang type system, and there area a few things the confuse me.

So I have been working on the http library to try to understand this and I have come across the following that makes no sense.

package main

import (
    "net/http"
    "fmt"
    "io/ioutil"
    "io"
)

func convert(closer io.Closer) ([]byte) {
    body, _ := ioutil.ReadAll(closer);
    return body
}

func main() {

    client := &http.Client{}
    req, _ := http.NewRequest("GET", "https://www.google.com", nil)

    response, _ := client.Do(req);

    body, _ := ioutil.ReadAll(response.Body)

    fmt.Println(body);
    fmt.Println(convert(response.Body))

}

The Go Playground

this is not about the fact that the convert function is not needed it is the fact that the response body is of type io.closer and the ioutil.Readall takes a io.reader, yet I can pass it in, in one instance but not if in another. Is there something that I am missing that is magically happening.

I know that the closer technically passes the reader interface as it implements the the Read method but that should be true in both the function and the main body.

Any insight would be great.

Thanks

  • 写回答

1条回答 默认 最新

  • dongtuo3370 2017-07-20 12:46
    关注

    it is the fact that the response body is of type io.closer

    No, it is not. Declaration of Request.Body is at http.Request:

    Body io.ReadCloser
    

    The Request.Body field is of type io.ReadCloser, it is both an io.Reader and an io.Closer.

    Since it is an io.Reader (dynamic value of Request.Body implements io.Reader), you may use / pass it where an io.Reader is required, e.g. to ioutil.ReadAll().

    Since it also implements io.Closer, you can also pass it where io.Closer is required, like your convert() function.

    But inside convert the closer param has static type io.Closer, you can't use closer where an in.Reader is required. It might be (and in your case it is) that the dynamic type stored in closer also implements io.Reader, but there is no guarantee for this. Like in this example:

    type mycloser int
    
    func (mycloser) Close() error { return nil }
    
    func main() {
        var m io.Closer = mycloser(0)
        convert(m)
    }
    

    In the above example closer inside convert() will hold a value of type mycloser, which truly does not implement io.Reader.

    If your convert() function intends to treat its parameter also as an io.Reader, the parameter type should be io.ReadCloser:

    func convert(rc io.ReadCloser) ([]byte, error) {
        body, err := ioutil.ReadAll(rc)
        if err != nil {
            return body, err
        }
        err = rc.Close()
        return body, err
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?