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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler