实现一个发出ASCII字符"A"的无限流的Reader类型.
我不明白这个问题,如何发出字符'A'?我应该在哪个变量中设置该字符?
这是我试过的:
package main
import "code.google.com/p/go-tour/reader"
type MyReader struct{}
// TODO: Add a Read([]byte) (int, error) method to MyReader.
func (m MyReader) Read(b []byte) (i int, e error) {
b = append(b,'A') // this is wrong..
return 1, nil // this is also wrong..
}
func main() {
reader.Validate(MyReader{}) // what did this function expect?
}
Run Code Online (Sandbox Code Playgroud)
Kok*_*zzu 33
啊,我理解XD
我认为最好说:"将所有值重写[]byte为'A's"
package main
import "code.google.com/p/go-tour/reader"
type MyReader struct{}
// TODO: Add a Read([]byte) (int, error) method to MyReader.
func (m MyReader) Read(b []byte) (i int, e error) {
for x := range b {
b[x] = 'A'
}
return len(b), nil
}
func main() {
reader.Validate(MyReader{})
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5764 次 |
| 最近记录: |