With Go being strict about unused variables why is this ok ?

polaris · · 601 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><a href="https://play.golang.org/p/ceQ9w5GjUQ">https://play.golang.org/p/ceQ9w5GjUQ</a></p> <p>Here the function &#34;fun&#34; takes in &#34;boom&#34; variable of type string, but makes no use of it in the function body. Still the code compiles and run&#39;s fine. Why is that so taking into account Go&#39;s strict rules about unused variables. Though it looks totally harmless, could it have any side effects ?</p> <hr/>**评论:**<br/><br/>james-h: <pre><p>If you want to create an interface, then you probably don&#39;t know exactly how each concrete implementation of that interface will behave, and so it would be unreasonable to require all implementations to make use of all variables. Since methods are functions, the same rule applies to all functions, even those that aren&#39;t methods.</p> <p>That being said, I think it would have made sense to force the following:</p> <pre><code>func boom(_ string) { fmt.Println(&#34;Having funnnnn&#34;) } </code></pre> <p>I think you&#39;re right that it&#39;s sort of inconsistent of Go to allow unused parameters.</p></pre>gkx: <pre><p>I think it&#39;s definitely a good point to force the underscore. I wonder if that&#39;s a change that would be reasonable or if it would break too much code.</p></pre>james-h: <pre><p>It certainly would, so don&#39;t hold your breath. This feature won&#39;t make its way into Go 1.x.</p></pre>eatingthenight2: <pre><p>I was amazed the first time I used _ as a parameter and go let me after yelling at me for just about everything else when I was learning it.</p></pre>natefinch: <pre><p>Yep, I agree.</p></pre>klaaax: <pre><p>well, no Go works the same way for variables and functions</p> <p>AFAIK you cannot declare local functions if they are not closures ,but you can declare closures on the local scope.</p> <p>AND</p> <p>if you declare a variable on the global scope, even a private one, you don&#39;t have to use it.</p> <pre><code> package main var x = 2 func main(){} </code></pre> <p>will compile without errors.</p></pre>skelterjohn: <pre><p>Two things come into play, here.</p> <p>First, for a method (which is basically the same thing as a function) to help a type satisfy an interface, it has to have all the parameters whether it uses them or not.</p> <p>Second, given that we must have all parameters, we can&#39;t have <em>some</em> of them named and <em>some</em> of them unnamed. It would be grammatically ambiguous since we can have &#34;func foo(x, y string)&#34;. Is x a type or is it a string? The grammar says it&#39;s a string.</p></pre>natefinch: <pre><p>Well, you can do func (foo) Bar(_, y string) to satisfy an interface but show that you&#39;re not using x.</p></pre>skelterjohn: <pre><p>Oh my, I didn&#39;t realize that was possible. I figured it would be a compile error :)</p></pre>pierrrre: <pre><p>interface</p></pre>

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

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