I have a beginner's question about structs and pointers.

agolangf · · 780 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;ve not used a language with pointers before, and this part is throwing me. Say I have a struct type.</p> <pre><code>type person struct { name string } </code></pre> <p>And a function that returns a new pointer to a struct of that type.</p> <pre><code>func getSarah() *person { sarah := &amp;person{name: &#34;Sarah Palmer&#34;} return sarah } </code></pre> <p>I call <code>sarah := getSarah()</code>. <code>sarah</code> is now a pointer to a memory location containing a person struct.</p> <p>But if I call <code>fmt.Println(sarah.name)</code>, that prints Sarah&#39;s name.</p> <p>Why does this happen? <code>sarah</code> is a pointer, just a memory address. Why can I call <code>.name</code> on a memory address without dereferencing it to get at the actual data it points to?</p> <hr/>**评论:**<br/><br/>fakeNAcsgoPlayer: <pre><p>x *T. (x is var of type pointer to T, where T can be anything ).</p> <p>x.a and x-&gt;a are same in Go. No difference unlike C and C++. </p></pre>faiface: <pre><p>pointer.field is actually a syntactic sugar for (*pointer).field in Go. So it is dereferencing. This syntactic sugar exists to reduce typing.</p></pre>Bake_Jailey: <pre><p>I think the other comments are answering different questions than you&#39;re asking.</p> <p>Go does not have something like <code>-&gt;</code> in C or C++ to access members of things you have pointers to, just <code>.</code>. You don&#39;t need to &#34;dereference&#34; anything, just access the struct&#39;s member through one simplified syntax.</p> <p>See the language spec for selectors: <a href="https://golang.org/ref/spec#Selectors" rel="nofollow">https://golang.org/ref/spec#Selectors</a></p></pre>spiritofthetempest: <pre><p>There&#39;s a simple syntax rule that transforms . on a pointer to first dereference the pointer like -&gt; would in C. The same thing happens when you pass a pointer to a method that expects a value. </p></pre>George3d6: <pre><p>A small mention here, when people say &#39;pointer&#39; (and I believe in go this is the only meaning for pointer), they generally mean &#39;pointer to dynamically allocated memory&#39;. </p> <p>So, in that sense, every language that you probably used until now, languages like java, python or js basically &#39;force&#39; you to use a pointer for everything, that is to say, all objects will be heap allocated and you will be working only with pointers to those (garbage collected) objects.</p></pre>deusmetallum: <pre><p>It&#39;s simply written to work that way, so you don&#39;t have to worry too much about what you&#39;re getting back from a function.</p></pre>9nut: <pre><p>because that&#39;s how fmt.Println is designed to work; that&#39;s to say it isn&#39;t inherent in the language, but a design decision in package fmt. look for the description of &#34;%v&#34; in: <a href="https://golang.org/pkg/fmt/" rel="nofollow">https://golang.org/pkg/fmt/</a></p> <p>the specific part starts with this paragraph: &#34;<em>For compound objects, the elements are printed using these rules, recursively, laid out like this:</em>&#34;</p></pre>

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

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