Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9063323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:58:08+00:00 2026-06-16T15:58:08+00:00

I tried to implement a very trivial io.Reader in Go: package main import (

  • 0

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 to apply ROT13 to the string that it is reading, but for now, I am just trying to create a very trivial io.Reader that matches the proper interface.

When I run this program, it never halts. Why? Where do I have an infinite loop?


Update: I tried altering the data splice via the for loop below, but it doesn’t seem to actually alter the splice. Do I need to somehow copy over data?

package main

import (

    "io"
    "os"
    "strings"
)

type rot13Reader struct {
    r io.Reader
}

func (r *rot13Reader) Read(data []byte) (int, error) {
    bytesRead, err := r.r.Read(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)
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:58:10+00:00Added an answer on June 16, 2026 at 3:58 pm

    io.Copy calls your Read() method continuously.

    From the docs:

    “Copy copies from src to dst until either EOF is reached
    on src or an error occurs. It returns the number of bytes
    copied and the first error encountered while copying, if any.”

    You need to either send EOF or return an error.

    Here’s an updated version that doesn’t endless 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 from 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 := r.r.Read(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")
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to implement GAE's webapp2 session, but there seems very little documentation about
While trying to learn c++, I tried to implement class representing very basic trie.
I tried to implement this example to write and read data from internal storage,
I tried to implement the Hover effect for images just like Google Images in
I tried to implement __concat__ , but it didn't work >>> class lHolder(): ...
I tried to implement XOR swap in python. x,y= 10,20 x,y,x = x^y,x^y,x^y print('%s
I tried to implement the following in less: nav > ul > li:first-child using:
I tried to implement FizzBuzz in DCPU-16. I use this web emulator: http://mappum.github.com/DCPU-16/ (repository:
I once tried to implement Comet in PHP. Soon, I found that PHP is
I have tried to implement css sticky footer on my page but it doesn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.