<p><a href="https://play.golang.org/p/iwTXTKSn4W" rel="nofollow">this won'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'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>"<a href="https://golang.org/ref/spec#Keywords" rel="nofollow">The following keywords are reserved and may not be used as identifiers.</a>"</em></p></pre>Mteigers: <pre><p>'type' always gets me. Always am trying to do things like Member.Type, Session.Type. But nope, have to settle with 'Typ' or Member.MemberType. </p></pre>neoasterisk: <pre><p>What's wrong with Type? It'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
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传