pointer receivers vs value receivers

xuanbao · · 442 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>First time posting here, but I wanted to get some feedback/corrections on a small blog post I made to help clear up my own confusions with the topic. I researched a lot for it, but I&#39;m concerned I might be missing something in my fundamental understanding of the topic.</p> <p>Here is a link to it, any feedback is appreciated: <a href="http://www.phizzle.space/golang/2017/01/27/go-pointer-receivers-vs-value-receivers.html" rel="nofollow">http://www.phizzle.space/golang/2017/01/27/go-pointer-receivers-vs-value-receivers.html</a></p> <hr/>**评论:**<br/><br/>joncalhoun: <pre><p>First piece of feedback - variables in go are camel case or Pascal case, not snake case. Update <code>status_code</code> in your example to be <code>statusCode</code></p></pre>joncalhoun: <pre><p>Remaining feedback:</p> <ol> <li><p>Read this: <a href="https://golang.org/doc/faq#methods_on_values_or_pointers" rel="nofollow">https://golang.org/doc/faq#methods_on_values_or_pointers</a> - it covers the topic pretty well.</p></li> <li><p>Mentioned in the previous link, but it might be worth noting that if you need a pointer receiver for a single method of your type, you should just use pointer receivers for all of the methods for consistency.</p></li> <li><p>Technically there is a difference between a pointer receiver and a value receiver for slices. The former can adjust the <em>length</em> of the slice, whereas the latter cannot. Both can affect values of the array backing the slice, which is why your example works, but both cannot append a new item to the slice.</p></li> </ol> <p>All in all the article reads well and does a pretty good job of introducing the differences between pointer and value receivers, but I definitely think the second point is worth stressing so that people know to keep their receivers consistent. </p></pre>phizzle5: <pre><p>thanks for the detailed feedback! I did not know all that special info about slices.</p></pre>heraclmene: <pre><pre><code>When a method is a value receiver, it means that you cannot modify the struct fields because the struct is being passed by value. </code></pre> <p>You can, it will only modify that method&#39;s struct fields. It is also best practice not to mix pointer receivers and value receivers; use one or the other depending on your use case (as mentioned by <a href="/u/jancalhoun" rel="nofollow">/u/jancalhoun</a>). </p></pre>phizzle5: <pre><p>Thank you!</p></pre>hobbified: <pre><p>I would say that a method <em>has</em> a pointer or value receiver, not that it <em>is</em> one. The &#34;receiver&#34; is the parameter that gets the object the method was called on. </p> <p>Also, <a href="http://stackoverflow.com/questions/33587227/golang-method-sets-pointer-vs-value-receiver/33591156#33591156" rel="nofollow">here&#39;s</a> something I wrote a while back on the question of when you&#39;re able to call each type of method, and how they go towards satisfying interfaces.</p></pre>phizzle5: <pre><p>thanks for the share, will check it out.</p></pre>alasijia: <pre><p>pointers are also values, so the terminology of <strong>value receiver</strong> is not good. A better alternative is &#34;non-pointer receiver&#34;. </p> <p>Receivers are just special parameters, which means they are also passed by value.</p></pre>hobbified: <pre><p>It&#39;s used by the language spec, and everyone knows what it means.</p></pre>alasijia: <pre><p>indeed.</p> <p>But there is not a definition for &#34;value receiver&#34; in spec.</p></pre>

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

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