eddycjy / blog

煎鱼的博客,有点忙,传送门:https://eddycjy.com
3.05k stars 431 forks source link

posts/go/slice-discuss/ #159

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

Go 切片这道题,吵了一个下午!

https://eddycjy.com/posts/go/slice-discuss/

zhengpeiqiang commented 2 years ago

大佬,猜一下结果是什么

func main() { s := make([]int, 0) //s = []int{0, 10, 100} s = append(s, 0) s = append(s, 10) s = append(s, 100) fmt.Printf("main 内存地址:%p\n", s) hello(s) fmt.Println(s[0:len(s)]) fmt.Println(cap(s)) fmt.Println(s[0:cap(s)]) }

func hello(s []int) { fmt.Printf("hello 内存地址:%p\n", s) s = append(s, 1000) s = append(s, 10000) s = append(s, 100000) s = append(s, 1000000) s = append(s, 10000000) s[0] = 1000 }

fun4wut commented 2 years ago

图挂了