Passing structs by pointer to functions

blov · · 363 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m fairly new to the Go language, so apologies if this has been talked about but I&#39;m having difficulty finding a clear and concise resource on this. Let&#39;s say that I create a struct in main, and I want to pass that struct by pointer to a function, so that it modifies the original struct. When I try doing this, I get an &#34;invalid indirect&#34; error and I&#39;m not totally sure what that means. Can anyone give me a direction on this? </p> <p>An example of the code I&#39;m running: <a href="https://play.golang.org/p/JNHQS4yBlfX" rel="nofollow">https://play.golang.org/p/JNHQS4yBlfX</a> </p> <hr/>**评论:**<br/><br/>tcrypt: <pre><p>Make sure that you&#39;re using <code>&amp;</code> and not <code>*</code> to get the pointer of your struct.</p> <p><a href="https://play.golang.org/p/7342qnxZeHS" rel="nofollow">https://play.golang.org/p/7342qnxZeHS</a></p></pre>titpetric: <pre><p>The correct answer that would help OP out should be:</p> <p>new() already produces a pointer to the struct. This means, when you&#39;re passing &#39;&amp;variable&#39;, you&#39;re actually creating a pointer to a pointer. To make your example work, you should just remove &#39;&amp;&#39;, or declare the original variable with something like <code>variable := Struct{}</code> (that is a concrete value and not a pointer).</p> <p>FTFY</p></pre>Sythe2o0: <pre><p>tcrypt posted their response before OP gave their code, and so had to guess what they were doing, hence didn&#39;t say anything about <code>new</code>.</p></pre>titpetric: <pre><p>I only read the main post and tcrypts comment, wasn’t aware op added code later on.</p></pre>ledongthuc1: <pre><p>main() { var s1 StructType anotherFunc(&amp;s1) }</p> <p>anotherFunc(s1 *StructType) { s1.UpdatedField = &#34;abc&#34; }</p></pre>MinnieAppleEater: <pre><p>when I try this, I get the following error:</p> <p>cannot use &amp;s1 (type **StructType) as type *StructType in argument to anotherFunc</p></pre>jerf: <pre><p>Use the playground to show your exact problem, and helping you will be a lot easier than trying to guess the source code that produced the given error.</p></pre>MinnieAppleEater: <pre><p><a href="https://play.golang.org/p/JNHQS4yBlfX" rel="nofollow">https://play.golang.org/p/JNHQS4yBlfX</a></p></pre>animouZz: <pre><p>When you use &#34;new&#34; you create a pointer to the struct created. So when you do &amp;thingy you are passing a **Thingy and not a *Thingy. You should pass thingy as is or declare it as:</p> <p>var thingy Thingy </p></pre>MinnieAppleEater: <pre><p>So by using the new keyword am I creating a new struct object and ALSO a pointer to it at the same time? </p></pre>Sythe2o0: <pre><p><code>new</code> returns a pointer to a zero-valued struct. </p></pre>Sythe2o0: <pre><p>I&#39;m not sure exactly what you&#39;re doing because I haven&#39;t seen your code, but does <a href="https://stackoverflow.com/questions/20890850/why-is-a-invalid-indirect" rel="nofollow">this stackoverflow</a> represent your problem (and solve it)?</p></pre>

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

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