dongshan4316 2016-08-13 09:08
浏览 71
已采纳

Gomt append()受fmt.Println()影响的奇怪行为

I noticed some strange behavior with Golang's append(). I understand the basic concept of how slice capacity affects whether a new underlying array gets allocated, but why is whether or not I use a fmt.Println() AFTER the append has happened affecting the result of the append?

package main

import "fmt"

func main() {
    a := []byte("AAA")

    b := append(a, []byte("BBB")...)
    fmt.Println(" a: ", string(a), " b: ", string(b))

    c := append(a, []byte("CCC")...)
    fmt.Println(" a: ", string(a), " b: ", string(b), " c: ", string(c))

    fmt.Println(&b) //try commenting this out and in and running the program
}

Link to run code here: https://play.golang.org/p/jJ-5ZxTBIn

  • 写回答

2条回答 默认 最新

  • dongyupen6269 2016-08-13 10:40
    关注

    You're right: That is because The Go Playground version is old (go1.6.2), use new version.


    The correct output (using go version go1.7rc6) is:

     a:  AAA  b:  AAABBB
     a:  AAA  b:  AAACCC  c:  AAACCC
    

    1- The Go Playground (go1.6.2):

    package main
    
    import "fmt"
    
    func main() {
        a := make([]byte, 100, 1000)
        a = []byte("AAA")
    
        b := append(a, []byte("BBB")...)
        fmt.Println(" a: ", string(a), " b: ", string(b))
    
        c := append(a, []byte("CCC")...)
        fmt.Println(" a: ", string(a), " b: ", string(b), " c: ", string(c))
    
        //fmt.Println(&b) //try commenting this out and in and running the program
    }
    

    output:

     a:  AAA  b:  AAABBB
     a:  AAA  b:  AAABBB  c:  AAACCC
    

    2- The Go Playground (go1.6.2):

    package main
    
    import "fmt"
    
    func main() {
        a := make([]byte, 100, 1000)
        a = []byte("AAA")
    
        b := append(a, []byte("BBB")...)
        fmt.Println(" a: ", string(a), " b: ", string(b))
    
        c := append(a, []byte("CCC")...)
        fmt.Println(" a: ", string(a), " b: ", string(b), " c: ", string(c))
    
        fmt.Println(&b) //try commenting this out and in and running the program
    }
    

    output:

     a:  AAA  b:  AAABBB
     a:  AAA  b:  AAACCC  c:  AAACCC
    &[65 65 65 67 67 67]
    

    using go version go1.7rc6:

    package main
    
    import "fmt"
    
    func main() {
        a := make([]byte, 100, 1000)
        a = []byte("AAA")
    
        b := append(a, []byte("BBB")...)
        fmt.Println(" a: ", string(a), " b: ", string(b))
    
        c := append(a, []byte("CCC")...)
        fmt.Println(" a: ", string(a), " b: ", string(b), " c: ", string(c))
    
        //fmt.Println(&b) //try commenting this out and in and running the program
    }
    

    output :

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

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝