doxrxwn2252 2017-08-21 16:27
浏览 24
已采纳

附加到切片中的for循环中具有意外结果

It could be a silly question but when I try to append a []byte slice to a [][]byte slice I get weird results.

Here is my code:

func Normalizer(s string) (ss [][]byte) {

    ss = make([][]byte, 0)
    // norm
    var ia norm.Iter

    ia.InitString(norm.NFC, s)

    for !ia.Done() {

        next := ia.Next()

        fmt.Println(next)
        // [226 128 139]
        // [227 128 129]
        // [39]
        // [226 128 153]
        // [46]
        // [44]
        // [63]
        // [33]
        // [92]
        // [10]
        // [226 128 153]
        // ...
        ss = append(ss, next)

    }
    ia.Done()


    fmt.Println(ss)
    return
}

I'm expecting somethin like this:

// [[226 128 139] [227 128 129] [39] [226 128 153] [46] [44] [63] [33] [92] [10] [226 128 153]...] 

but instead i get this:

// [[226 129 128] [226 129 128] [226] [226 129 128] [226] [226] [226] [226] [226] [226] [226 129 128]...]

and I have no idea why. Help and explanation would be appreciated.

  • 写回答

1条回答 默认 最新

  • drxm72811 2017-08-21 16:45
    关注

    A slice is a struct with a pointer to an underlying array, a length, and a capacity.

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

    You are changing the underlying array after you have appended the slice struct. ia.Next() reuses its return buffer.

    For example,

    package main
    
    import (
        "fmt"
    
        "golang.org/x/text/unicode/norm"
    )
    
    func Normalizer(s string) (ss [][]byte) {
        ss = make([][]byte, 0)
        var ia norm.Iter
        ia.InitString(norm.NFC, s)
        for !ia.Done() {
            next := ia.Next()
            fmt.Println(string(next), &next[0])
            ss = append(ss, next)
        }
        fmt.Println()
        for i := range ss {
            fmt.Println(string(ss[i]), &ss[i][0])
        }
        fmt.Println()
        return
    }
    
    func main() {
        ss := Normalizer("abc")
        fmt.Printf("%s
    ", ss)
    }
    

    Output:

    a 0xc420092228
    b 0xc420092228
    c 0xc420092228
    
    c 0xc420092228
    c 0xc420092228
    c 0xc420092228
    
    [c c c]
    

    Replace a copy of a slice struct

    next := ia.Next()
    

    with a new slice struct with a new underlying array

    next := append([]byte(nil), ia.Next()...)
    

    For example,

    package main
    
    import (
        "fmt"
    
        "golang.org/x/text/unicode/norm"
    )
    
    func Normalizer(s string) (ss [][]byte) {
        ss = make([][]byte, 0)
        var ia norm.Iter
        ia.InitString(norm.NFC, s)
        for !ia.Done() {
            next := append([]byte(nil), ia.Next()...)
            fmt.Println(string(next), &next[0])
            ss = append(ss, next)
        }
        fmt.Println()
        for i := range ss {
            fmt.Println(string(ss[i]), &ss[i][0])
        }
        fmt.Println()
        return
    }
    
    func main() {
        ss := Normalizer("abc")
        fmt.Printf("%s
    ", ss)
    }
    

    Output:

    a 0xc4200120d0
    b 0xc4200120e8
    c 0xc420012108
    
    a 0xc4200120d0
    b 0xc4200120e8
    c 0xc420012108
    
    [a b c]
    

    References:

    Slice types

    Go Slices: usage and internals

    Arrays, slices (and strings): The mechanics of 'append'

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据