go append 的疑问

2023-02-27 11:46:28 +08:00
 echoless
package main

import "fmt"

func main() {
	// create a slice from an array
	x := [3]string{"A", "B", "С"}
	s := x[:] // a slice referencing the storage of x
	// x[1] = "O"
	t := append(s, "D") // append items to slice s
	x[1] = "O"
	fmt.Println("%+v", x)
	fmt.Println("%+v", s)
	fmt.Println("%+v", t)
}

https://go.dev/play/p/g-eGRJLteAH

t 为什么是 [A B С D]

我的理解是 t 在 s 的基础上加了个‘D' , x 改了 s, t 也要跟着变啊.

1980 次点击
所在节点    Go 编程语言
41 条回复
echoless
2023-02-28 12:40:21 +08:00
哪天 chatgpt, copilot 能写代码的时候, 估计又有高级程序员跳出来说, xxx 圈养了一堆 crud 都不懂, 只会 prompt 的 XX.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/919461

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX