如何打电话给读者.Validate(MyReader{}) 调用我的自定义读取方法?



https://github.com/golang/tour/blob/master/solutions/readers.go

package main
import "golang.org/x/tour/reader"
type MyReader struct{}
func (r MyReader) Read(b []byte) (int, error) { . //Q1) How is this method getting called?
//Q2) Its no where called in this source code
//Q3) What is the length of b ?
for i := range b { //Q4) Why isn't throwing an infinite loop ?
b[i] = 'A' 
}
return len(b), nil
}
func main() {
reader.Validate(MyReader{})
}

它调用 Read(b []byte( 查看源代码 这里 https://github.com/golang/tour/blob/master/reader/validate.go#L17

验证(io.读者(期望一个io。读取器,只需要一个 Read([]byte( 函数来填充接口。这就是你正在做的事情,所以验证可以打电话给你的读者。

最新更新