【发布时间】:2021-08-28 13:17:13
【问题描述】:
我是 Golang 的新手,我有以下代码 sn-p 和下面的输出,这让我很困惑。我期待append 将字符串复制到切片而不是替换它?所以我的预期输出应该是[[one] [two]] 为什么不这样做?
type StringList []interface{}
type GlobalStringList []StringList
var globalStringList GlobalStringList
var stringList StringList
myString := "one"
stringList = append(stringList, myString)
globalStringList = append(globalStringList, stringList)
stringList[0] = "two"
globalStringList = append(globalStringList, stringList)
fmt.Print(globalStringList)
// Output
// [[two] [two]]
【问题讨论】:
-
这可能会解决问题:blog.golang.org/slices
-
停止猜测,开始围棋之旅。