io (Reader & Wirter)

https://www.flysnow.org/2017/05/08/go-in-action-go-reader-writer.html

高度抽象的接口, 理解面向接口编程. 核心就是面向接口编程...

下面可以看看 ioutil.ReadAll 方法的实现

func readAll(r io.Reader, capacity int64) (b []byte, err error) {
    buf := bytes.NewBuffer(make([]byte, 0, capacity))
    // If the buffer overflows, we will get bytes.ErrTooLarge.
    // Return that as an error. Any other panic remains.
    defer func() {
        e := recover()
        if e == nil {
            return
        }
        if panicErr, ok := e.(error); ok && panicErr == bytes.ErrTooLarge {
            err = panicErr
        } else {
            panic(e)
        }
    }()
    _, err = buf.ReadFrom(r)
    return buf.Bytes(), err
}

有一点点理解面向接口编程的意思了...

你可能感兴趣的:(io (Reader & Wirter))