eddycjy / blog

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

posts/go/value-quote/ #169

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

群里又吵起来了,Go 是传值还是传引用?

https://eddycjy.com/posts/go/value-quote/

koushr commented 1 year ago

func main() { s := "脑子进煎鱼了" fmt.Printf("main 内存地址:%p\n", &s) hello(&s) }

func hello(s *string) { fmt.Printf("hello 内存地址:%p\n", &s) }

koushr commented 1 year ago

func main() { s := "脑子进煎鱼了" fmt.Printf("main 内存地址:%p\n", &s) hello(&s) }

func hello(s *string) { fmt.Printf("hello 内存地址:%p\n", &s) }

koushr commented 1 year ago

func main() { s := "脑子进煎鱼了" fmt.Printf("main 内存地址:%p\n", &s) hello(&s) }

func hello(s *string) { fmt.Printf("hello 内存地址:%p\n", &s) } hello里面不应该是这样打印吗? fmt.Printf("hello 内存地址:%p\n", s) 这样打印出来结果是一样的

jincheng9 commented 1 year ago

Go语言没有传引用的说法,可以参看我写的文章 https://mp.weixin.qq.com/s?__biz=Mzg2MTcwNjc1Mg==&mid=2247483678&idx=1&sn=27acd77cc05709b16cdd7e1a89470f38&chksm=ce124e71f965c767b642533547792836754f533d40b18fec9b970a83c35fae4b1c61ac4f005d&token=2029492652&lang=zh_CN#rd