https://github.com/golang/tour/blob/master/solutions/readers.gopackage mainimport "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{})}
1 回答

蠱毒傳說
TA貢獻(xiàn)1895條經(jīng)驗 獲得超3個贊
它調(diào)用 Read(b []byte) 在這里查看源代碼https://github.com/golang/tour/blob/master/reader/validate.go#L17
Validate(io.Reader) 需要一個 io.Reader,它只需要一個 Read([]byte) 函數(shù)來填充接口。這就是您正在做的事情,因此 Validate 可以調(diào)用您的讀者。
- 1 回答
- 0 關(guān)注
- 283 瀏覽
添加回答
舉報
0/150
提交
取消