doufei9946 2019-07-23 20:39
浏览 37
已采纳

读取功能完成后,不会更新Go rot13Reader缓冲区的行程

Here is my implementation of the exercise using strings.Map (the rot13 function is straight out of golang's docs). The issue is that the buffer does not seem to be modified after the Read function returns. Here is the code:

package main

import (
    "io"
    "os"
    "strings"
    "fmt"
)

type rot13Reader struct {
    r io.Reader
}

func (reader *rot13Reader) Read(b []byte) (int, error) {
    rot13 := func(r rune) rune {
        switch {
        case r >= 'A' && r <= 'Z':
            return 'A' + (r-'A'+13)%26
        case r >= 'a' && r <= 'z':
            return 'a' + (r-'a'+13)%26
        }
        return r
    }
    n, err := reader.r.Read(b)
    result := []byte(strings.Map(rot13, string(b)))
    b = []byte(result)
    fmt.Println(string(b))
    return n, err
}

func main() {
    s := strings.NewReader("Lbh penpxrq gur pbqr!")
    r := rot13Reader{s}
    io.Copy(os.Stdout, &r)
}

and the output:

You cracked the code!
Lbh penpxrq gur pbqr!You cracked the code!

Clearly the buffer has been modified in the Read function, but it does not seem to be the case after it returns. If I were to comment out the fmt.Println(string(b)), the output would just be:

Lbh penpxrq gur pbqr!

Is there something quirky about Readers and Writers that I should know about?

  • 写回答

2条回答 默认 最新

  • douyanguo7964 2019-07-23 22:55
    关注

    In Go, all arguments are passed by value, as if by assignment to the parameter or receiver (a shallow copy).

    In Go, a slice is implemented as

    type slice struct {
        array unsafe.Pointer
        len   int
        cap   int
    }
    

    When the slice is passed by value, after you return, you will not see any changes you make to the copy of the struct fields. You will only see any changes to elements of the underlying array.


    In your case, you overwrite b (array, cap, len), a copy.

    b = []byte(result)
    

    The copy is is discarded when you return.


    What you want to do is change elements of b's array.

    For example,

    package main
    
    import (
        "io"
        "os"
        "strings"
    )
    
    func rot13(b byte) byte {
        switch {
        case b >= 'A' && b <= 'Z':
            return 'A' + (b-'A'+13)%26
        case b >= 'a' && b <= 'z':
            return 'a' + (b-'a'+13)%26
        }
        return b
    }
    
    type rot13Reader struct {
        r io.Reader
    }
    
    func (reader *rot13Reader) Read(b []byte) (int, error) {
        n, err := reader.r.Read(b)
        b = b[:n]
        for i := range b {
            b[i] = rot13(b[i])
        }
        return n, err
    }
    
    func main() {
        s := strings.NewReader("Lbh penpxrq gur pbqr!")
        r := rot13Reader{s}
        io.Copy(os.Stdout, &r)
    }
    

    Playground: https://play.golang.org/p/0LDYmzrrgty

    Output:

    You cracked the code!
    

    The Go Blog: Go Slices: usage and internals

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)