Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tour of Go exercise #22: Reader what does mean the question?

Tags:

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.. } 
like image 332
Kokizzu Avatar asked Jan 08 '15 11:01

Kokizzu


1 Answers

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{}) } 
like image 140
Kokizzu Avatar answered Oct 09 '22 16:10

Kokizzu