Be careful what you name your method receivers

blov · · 383 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><a href="https://play.golang.org/p/iwTXTKSn4W" rel="nofollow">this won&#39;t compile</a>. The reason is simple <code>select</code> is a keyword and even though you define it as a receiver to <code>foo</code> it translates to <code>select(*foo, key string)</code> so remember not to name your method receivers with keywords.</p> <p>if you have a better explanation I would like to know.</p> <hr/>**评论:**<br/><br/>xrstf: <pre><p><code>s/method receivers/identifiers/g</code> - Using reserved names for anything, even when possible, is generally a bad idea. I wouldn&#39;t even have expected Go to allow <code>select</code> as the name for a receiver.</p></pre>gernest_: <pre><p>I used select accidentally, and I was confused when it failed to compile, and the error message is kinda vague <code>syntax error: unexpected select, expecting name or (</code> is not very clear that select is a reserved word to me.</p></pre>tdewolff: <pre><p>Sounds a lot more clear that your average C++ compiler error though!</p></pre>neoasterisk: <pre><p><em>&#34;<a href="https://golang.org/ref/spec#Keywords" rel="nofollow">The following keywords are reserved and may not be used as identifiers.</a>&#34;</em></p></pre>Mteigers: <pre><p>&#39;type&#39; always gets me. Always am trying to do things like Member.Type, Session.Type. But nope, have to settle with &#39;Typ&#39; or Member.MemberType. </p></pre>neoasterisk: <pre><p>What&#39;s wrong with Type? It&#39;s perfectly fine to use it. The reserved keyword is <code>type</code>.</p></pre>synalx: <pre><p>Honestly, I find that a little weird, though - in a struct you can have a public member named <code>Type</code> but not a private one.</p></pre>

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

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