demystifying pointers

polaris · · 894 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m currently helping to introduce Go to people at work, and one thing that seems to scare people is pointers. Most of the team come from a Python or Java background, where you don&#39;t have to make decisions about pointers vs non-pointers. AFAICT, these fears seem to stem from negative experiences with C/C++ (which probably drew them towards Python/Java/... in the first place). I figure I should run a short session on demystifying pointers, and I&#39;d like to explore the best ways to explain.</p> <p>Here&#39;s what I plan to say. I&#39;m looking for feedback, please.</p> <ol> <li><p>For starters, I usually try to explain to people that pointers in Go give you the choice about whether to make something nullable or not (pointer vs non-pointer). In many cases, you don&#39;t need nullable types, and nullables just increase the risk of null-pointer errors. In Java, you can&#39;t make that choice - everything&#39;s nullable - except &#39;primitives&#39;, but the primitives behave very differently to their &#39;object&#39; equivalents. Go&#39;s approach is more clear, consistent and flexible.</p></li> <li><p>It&#39;s important to underline the difference between pass-by-reference and pass-by-value, but I&#39;d imagine people understand this already. I guess it&#39;s good to have an awareness of the differences in RAM usage, but I don&#39;t think it&#39;s crucial to get too technical about this aspect.</p></li> <li><p>When defining types, I tend to use some basic rules of thumb, so I&#39;m not agonising over pointers vs values:</p></li> </ol> <p>3(a). When defining local variables and struct members, I favour values as much as possible - reducing the chance of nil pointer errors.</p> <p>3(a&#39;). A common exception for this, is when you care about the difference between a nil and a zero-value (e.g. nil vs &#34;&#34;). In these cases, use pointers. Nullable variables are important less often than I would have guessed, coming from other languages. But still, sometimes it&#39;s necessary.</p> <p>3(b). Conversely, when defining structs, I favour referencing and defining their methods with pointer receivers. Whenever you want to affect the state of the struct within a method or function, this is essential. &#39;Value receivers&#39; only get a copy of the calling object, so any modifications would not be seen by the calling code.</p> <p>3(b&#39;). The exception to this pointer-receiver rule, is when defining &#39;immutable&#39; types (not exactly immutable, but types which you&#39;d rather be stateless). For example, a &#39;service&#39; or a &#39;controller&#39; rarely needs modifying after initialisation, so, I can use value receivers and instances.</p> <p>3(b&#39;&#39;). Generally, with struct methods, be consistent. Mixing pointer receivers vs value receivers can lead to confusion. Likewise, be consistent with instantiation - a given struct should be referenced either as a pointer or a value variable. When you have pointer-receiver methods, always refer to your instances as pointers.</p> <p>What do you think? Is this roughly how you make these design decisions? Is there something important I&#39;m missing, or any better way of explaining this stuff?</p> <p>Ta</p> <hr/>**评论:**<br/><br/>simon-whitehead: <pre><p>Avoid the phrase &#34;pass by reference&#34; when explaining pointers. Pointers are passed by value like everything else.. they&#39;re cheap because pointers are a single &lt;word size of platform here&gt; copy. Anyway, it becomes confusing very quickly for newcomers.</p></pre>R2A2: <pre><p>Good point. Can you tell I used to be a Java dev? I forget that pointers &amp; references mean different things in C++</p> <p>Ta</p></pre>jkoudys: <pre><p>Personally I find Go (and C&#39;s) approach much simpler than Java&#39;s, for many of the reasons you describe. It sounds like what you&#39;re really trying to do is not just explore how pointers work in Go, but to sell your coworkers on the benefits of Go.</p> <p>You touch on it in #1, but I think this could go over well if you keep that theme and focus throughout covering your remaining points: how would this be done in Java/Python, and why is it better and more clear in Go? I&#39;d have a side-by-side comparison to Go at the end of each section just to cover the problems you can run into in Java that don&#39;t plague Go developers.</p></pre>R2A2: <pre><p>Yep, I see what you mean. I can easily show a java NPE on an uninitialised string variable, vs Go. Ta</p></pre>

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

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