doupang9614 2012-12-24 19:31
浏览 16
已采纳

为什么我的Go Reader无法正常工作?

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条回答 默认 最新

  • douzi6992 2012-12-24 19:40
    关注

    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...
    ")
    }
    

    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("
    Done copying...
    ")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?