5

小白求助,关于Go编译的顺序

 2 years ago
source link: https://studygolang.com/articles/35817?fr=sidebar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

小白求助,关于Go编译的顺序

shenhao-stu · 2天之前 · 468 次点击 · 预计阅读时间不到 1 分钟 · 大约8小时之前 开始浏览    

为什么bar()不是用main中的局部变量p,bar()不是在main()中使用的嘛?

v2-9d9b41fff81bf058036724b3f9b22fe2_b.jpeg
为什么main()匿名函数不等价于原先的写法呢?
v2-04d3a08c68ec67ab1719cb662bb160e7_r.jpg
个人理解是:bar()在main()函数外定义,所以使用的变量作用域应该为全局变量。但是如果按照编译器执行的顺序,为什么运行到bar()的时候,还能知道使用全局呢,不应该使用main()中的局部变量嘛?

有疑问加站长微信联系(非本文作者))

280

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK