What's going on with variable scope here?

blov · · 385 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I define a variable outside an if block, use short assignment (var,err := Func()) inside the if block, and test for a value on the variable. It does have one at that point. Then outside the if-block it reverts to nil.</p> <p>It&#39;s obviously a scope thing (unless it&#39;s a bug) but I didn&#39;t see it documented. Can someone point me to the literature on this behavior?</p> <p><a href="https://play.golang.org/p/N8APE96JQe" rel="nofollow">https://play.golang.org/p/N8APE96JQe</a> For reference.</p> <hr/>**评论:**<br/><br/>condanky: <pre><p>you are declaring a new variable with := inside the if statement. If you want to retain the value you need to 1) ignore the error which is bad. 2) declare an error variable and get rid of the short assignment. IE</p> <p>var foo *Thing if true { var err error foo, err = Test() if err != nil { return } fmt.Println(foo) } fmt.Println(foo)</p></pre>hackop: <pre><p>That&#39;s what I assumed was happening, but I didn&#39;t see where if-blocks are described as having their own scope. Inside the if-condition, it says the variables are scoped:</p> <pre><code>if local := Func();local != nil { local.prop = 1 } </code></pre> <p>But I figured using := inside the if-block itself wouldn&#39;t scope the variable to just that block.</p></pre>hobbified: <pre><p>Every block is a block.</p></pre>earless1: <pre><p>I think the reason you are seeing this is due to the <code>:=</code> I hacked the code up a little bit to show the point <a href="https://play.golang.org/p/TBm0piSYl0" rel="nofollow">https://play.golang.org/p/TBm0piSYl0</a></p></pre>hackop: <pre><p>Yeah, I figured it had to do with :=</p> <p>I was running under the assumption that := wouldn&#39;t create a new variable if one with the like name already exists. For instance, if you use var1 := Func(), and var1 already exists, you get a compiler error. At least one of the variables on the left of := has to be new.</p> <p>So, I assumed that := would use the existing variable and create a new one where applicable. Apparently it does not. Or something else happens within the if{} block.</p></pre>element131: <pre><blockquote> <p>:= wouldn&#39;t create a new variable if one with the like name already exists. </p> </blockquote> <p>if one with the like name already exists in the current block.</p> <p>You can &#34;shadow&#34; a variable like this:</p> <pre><code>x := 1 { fmt.Println(x) //x is in scope, so this works x := 2 //no x was declared in the current block, so this works fmt.Println(x) //the &#34;new&#34; x is in scope, so we get 2 } fmt.Println(x) //1 2 1 </code></pre> <p><a href="https://play.golang.org/p/JjR6uMseDC" rel="nofollow">https://play.golang.org/p/JjR6uMseDC</a></p></pre>hackop: <pre><p>Huh... well that&#39;s interesting. So, maybe I missed it when I was looking for info in the language spec. Do {} always denote a block where variables can be (or become) locally scoped? If so, I&#39;ll probably have to be more careful about keeping that in mind.</p></pre>element131: <pre><p>Yes, variable declared within braces are locally scoped. </p></pre>

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

385 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传