<p>I'm fairly new to the Go language, so apologies if this has been talked about but I'm having difficulty finding a clear and concise resource on this. Let'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 "invalid indirect" error and I'm not totally sure what that means. Can anyone give me a direction on this? </p>
<p>An example of the code I'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're using <code>&</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're passing '&variable', you're actually creating a pointer to a pointer. To make your example work, you should just remove '&', 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'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(&s1)
}</p>
<p>anotherFunc(s1 *StructType) {
s1.UpdatedField = "abc"
}</p></pre>MinnieAppleEater: <pre><p>when I try this, I get the following error:</p>
<p>cannot use &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 "new" you create a pointer to the struct created. So when you do &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'm not sure exactly what you're doing because I haven'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
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传