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 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?