Why is my Go Reader not working?

I tried to implement a very trivial io.Reader in Go:

 package main import ( "io" "os" "strings" ) type rot13Reader struct { r io.Reader } // Very trivial function I implemented. func (r *rot13Reader) Read(p []byte) (int, error) { return 5, nil // Return some trivial values for now. } func main() { s := strings.NewReader( "Lbh penpxrq gur pbqr!") r := rot13Reader{s} io.Copy(os.Stdout, &r) } 

In the end, I want rot13Reader apply ROT13 to the line it reads, but for now, I'm just trying to create a very trivial io.Reader that matches the correct interface.

When I run this program, it never stops. What for? Where do I have an infinite loop?


Update: I tried changing the splice of data using the for loop below, but it doesn't seem to actually change the splice. Do I need to somehow copy more data ?

 package main import ( "io" "os" "strings" ) type rot13Reader struct { r io.Reader } func (r *rot13Reader) Read(data []byte) (int, error) { bytesRead, err := rrRead(data) // Try to alter data... only without this for loop, text prints in standard output... odd. for i := 0; i < bytesRead; i++ { data[i] += 13 } return bytesRead, err } func main() { s := strings.NewReader( "Lbh penpxrq gur pbqr!") r := rot13Reader{s} io.Copy(os.Stdout, &r) } 
+4
source share
1 answer

io.Copy constantly calls your Read () method.

From the docs:

"Copy the copies from src to dst until you reach EOF on src or an error occurs. It returns the number of bytes and the first error that occurs when copying, if any."

You need to either send EOF or return an error.

Here's an updated version that does not have an infinite loop.

http://play.golang.org/p/UCfcw4S8yf

 package main import ( "io" "os" "strings" "fmt" ) type rot13Reader struct { r io.Reader } // Very trivial function I implemented. func (r *rot13Reader) Read(p []byte) (int, error) { return 5, io.EOF // Return some trivial values for now. } func main() { s := strings.NewReader( "Lbh penpxrq gur pbqr!") r := rot13Reader{s} io.Copy(os.Stdout, &r) fmt.Printf("Done copying...\n") } 

If you want to actually copy the string to the output. You need to read with rot13Reader.r and copy it to p in the Read () method.

http://play.golang.org/p/dSCauz0uTw

 package main import ( "io" "os" "strings" "fmt" ) type rot13Reader struct { r io.Reader } // Very trivial function I implemented. func (r *rot13Reader) Read(p []byte) (int, error) { i, err := rrRead(p) return i, err } func main() { s := strings.NewReader("Lbh penpxrq gur pbqr!") r := rot13Reader{s} io.Copy(os.Stdout, &r) fmt.Printf("\nDone copying...\n") } 
+8
source

All Articles