Exercise: Readers
Implement a Reader type that emits an infinite stream of the ASCII character 'A'.
I don't understand the question, how to emit character 'A'? into which variable should I set that character?
Here's what I tried:
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.. }
Ah I understand XD
I think it would be better to say: "rewrite all values in []byte
into '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{}) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With