Go 练习之旅 #22:Reader,问题是什么意思?
Tour of Go exercise #22: Reader, what does the question mean?
实现一个 Reader 类型,它发出无限的 ASCII 字符流 'A'。
我不明白这个问题,如何发出字符'A'?我应该将该字符设置到哪个变量中?
这是我尝试过的:
package main
import "golang.org/x/tour/reader"
type MyReader struct{}
// TODO: Add a Read([]byte) (int, error) method to MyReader.
func main() {
reader.Validate(MyReader{}) // what did this function expect?
}
func (m MyReader) Read(b []byte) (i int, e error) {
b = append(b, 'A') // this is wrong..
return 1, nil // this is also wrong..
}
啊我明白了XD
我认为这样说会更好:“将 []byte
中的所有值重写为 'A'
s”
package main
import "golang.org/x/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{})
}
所谓的答案对我不起作用,即使没有错别字。
像我一样尝试,那个字符串不会进入 b.
func (r MyReader) Read(b []byte) (int, error) {
return copy(b, "A"), nil
}
我的解决方案:每次只添加一个字节,使用闭包存储索引i
。
package main
import (
"golang.org/x/tour/reader"
)
type MyReader struct{}
func (mr MyReader) Read(b []byte) (int, error) {
i := 0
p := func () int {
b[i] = 'A'
i += 1
return i
}
return p(), nil
}
func main() {
reader.Validate(MyReader{})
}
io.Reader.Read
的作用是用从其源读取的数据写入给定的内存位置。
要实现 'A'
的流,该函数必须用 'A'
值写入给定的内存位置。
不需要填写input中提供的整个slice,它可以决定写入input slice的字节数(Read reads up to len(p) bytes into p
),必须return那个数字来表示给消费者要处理的数据长度。
按照惯例,io.Reader
通过 return 出现 io.EOF
错误来指示其结束。如果 reader 没有 return 错误,它就作为其消费者的无限数据源,永远无法检测到退出条件。
请注意,returns 0 bytes
读取的对 Read
的调用可能会发生,并不表示任何特殊情况,Callers should treat a return of 0 and nil as indicating that nothing happened;
Which makes this non-solution https://play.golang.org/p/aiUyc4UDYi2 因超时而失败。
关于这一点, return copy(b, "A"), nil
提供的解决方案确实是正确的。它编写了最低限度的要求,优雅地使用了内置函数和语法工具,而且它永远不会 return 出错。
实现一个 Reader 类型,它发出无限的 ASCII 字符流 'A'。
我不明白这个问题,如何发出字符'A'?我应该将该字符设置到哪个变量中?
这是我尝试过的:
package main
import "golang.org/x/tour/reader"
type MyReader struct{}
// TODO: Add a Read([]byte) (int, error) method to MyReader.
func main() {
reader.Validate(MyReader{}) // what did this function expect?
}
func (m MyReader) Read(b []byte) (i int, e error) {
b = append(b, 'A') // this is wrong..
return 1, nil // this is also wrong..
}
啊我明白了XD
我认为这样说会更好:“将 []byte
中的所有值重写为 'A'
s”
package main
import "golang.org/x/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{})
}
所谓的答案对我不起作用,即使没有错别字。 像我一样尝试,那个字符串不会进入 b.
func (r MyReader) Read(b []byte) (int, error) {
return copy(b, "A"), nil
}
我的解决方案:每次只添加一个字节,使用闭包存储索引i
。
package main
import (
"golang.org/x/tour/reader"
)
type MyReader struct{}
func (mr MyReader) Read(b []byte) (int, error) {
i := 0
p := func () int {
b[i] = 'A'
i += 1
return i
}
return p(), nil
}
func main() {
reader.Validate(MyReader{})
}
io.Reader.Read
的作用是用从其源读取的数据写入给定的内存位置。
要实现 'A'
的流,该函数必须用 'A'
值写入给定的内存位置。
不需要填写input中提供的整个slice,它可以决定写入input slice的字节数(Read reads up to len(p) bytes into p
),必须return那个数字来表示给消费者要处理的数据长度。
按照惯例,io.Reader
通过 return 出现 io.EOF
错误来指示其结束。如果 reader 没有 return 错误,它就作为其消费者的无限数据源,永远无法检测到退出条件。
请注意,returns 0 bytes
读取的对 Read
的调用可能会发生,并不表示任何特殊情况,Callers should treat a return of 0 and nil as indicating that nothing happened;
Which makes this non-solution https://play.golang.org/p/aiUyc4UDYi2 因超时而失败。
关于这一点,return copy(b, "A"), nil
提供的解决方案确实是正确的。它编写了最低限度的要求,优雅地使用了内置函数和语法工具,而且它永远不会 return 出错。