Pass structs as parameters to functions

xuanbao · · 1707 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>So this question is not about how to pass structs as parameters to functions. I learned that from <a href="https://tour.golang.org/methods/2">this exercise</a> and seeing examples on Stack Overflow. This question is, however, about where in the official documentation of Go this info is documented.</p> <p>I feel like I&#39;ve searched through the documentation but do not see it explained, which seems odd.</p> <p>Did I miss something in the offical docs? Can anyone confirm and/or point me in the right direction?</p> <hr/>**评论:**<br/><br/>lstokeworth: <pre><p>There&#39;s no specific documentation for struct parameters because struct types are handled no differently than any other type.</p></pre>wubrgess: <pre><p>worth noting though is that you will almost always want a <em>pointer</em> to a struct to be passed into a function rather than the struct literal. Coming from C, that saves stack space (I&#39;m not too sure about Go). The real non-performance reason to prefer it is because then you can modify the struct that&#39;s passed in because it&#39;s not actually copied and you then operate on the original. If you specifically want to prohibit that possibility inside of you functions, that&#39;s a different matter. Another thing to note that I learned recently is that interface arguments are <em>always</em> passed as pointers so there&#39;s no need to specify a pointer to an interface in a function signature.</p></pre>drvd: <pre><p>Recommending to do something one way &#34;always&#34; is wrong almost always. &#34;Pointer vs value&#34; is a: It depends.</p></pre>kostix: <pre><p>Oh, please...</p> <ul> <li><p>No, quite often, you&#39;d want to pass struct values &#34;as is&#34;--making copies at the call site. If your struct type contains just a few small fields, that&#39;s absolutely OK, and won&#39;t result in performance drops.</p></li> <li><p>&#34;Interface arguments&#34; are <em>not</em> &#34;passed as pointers&#34;.</p> <p>If a formal argument of a function has interface type, the actual argument might first get &#34;boxed&#34; (but only if needed) into <em>a value</em> of interface type, which presently, in the Go implementation known as <code>gc</code> (that one originally from Google), is a struct of two pointers, and that value will be <em>copied</em> at the call site -- just like any other value would.</p> <p>So yes, most of the time pointers to interfaces are useless but not due to the reasons you described they are.</p></li> </ul></pre>titpetric: <pre><p>beginner issues: a slice is a pointer to an underlying array, so people think that because the slice itself or enclosing structure is not passed as a pointer, they assume that all the slices would be copied to the function. Alas, modifying the slice from such a function, would leave the slice modified after the function return as well.</p> <p>not only should you know <em>when</em> to use pointers, you should also know <em>when you actually are using pointers without explicit intent</em>.</p> <p>Edit: <a href="https://scene-si.org/2017/08/06/the-thing-about-slices/" rel="nofollow">Go tips and tricks: the thing about slices</a>.</p></pre>paradoxops: <pre><p>Your statement doesn&#39;t appear to be true to me.</p> <p>If I pass the parameter <strong>word</strong> of the string type into a function called <strong>greeting</strong>, I would type the following:</p> <pre><code>greeting(word string) </code></pre> <p>That&#39;s not what&#39;s happening with struct. With struct (as in the example from the Go Tour), you&#39;re taking the parameter <strong>v</strong> and placing the name of the struct (Vertex) after it:</p> <pre><code>Abs(v Vertex) </code></pre> <p>That&#39;s not how all the data types I&#39;ve seen so far in the tutorial are passed in as a parameter to a function. For your statement to be true, the code snippet in question would need to look like this:</p> <pre><code>Abs(v struct) </code></pre> <p>I know I&#39;m probably missing something but just trying to clarify my point of confusion with you. To recap: I&#39;ve been seeing/expecting:</p> <pre><code>&lt;func&gt;(&lt;name of param&gt; &lt;type&gt;) </code></pre> <p>but this example is showing me</p> <pre><code>&lt;func&gt;(&lt;name of param&gt; &lt;name of struct&gt;) </code></pre></pre>paul2048: <pre><p>named types vs unnamed types :</p> <p><code>type Vertex struct { X,Y int }</code> is a type definition of the named type Vertex.</p> <p>so you just reused the type Vertex you just defined.</p> <p><code>func Abs(v struct){}</code> is not valid Go syntax anyway.</p> <p><code>func Abs(v struct { X,Y int }){}</code> would be valid, but why would you bother typing struct {X,Y int} over and over again when you can just reference it by a name? imagine your struct having 30 fields ...</p> <blockquote> <p>but this example is showing me &lt;func&gt;(&lt;name of param&gt; &lt;name of struct&gt;) </p> </blockquote> <p>No , it&#39;s not name of struct , it is the type Vertex you just defined. It&#39;s a named struct type, not the name of struct.</p> <p>edit : why would it be surprising? did you ever write C code or Java code ? if you define a class Foo, then you reference the classname Foo as the type of the argument, you just don&#39;t use the keyword class .</p></pre>paradoxops: <pre><p>Man this explanation makes a lot of sense. I had to read up a little on named types vs unnamed types. That, in addition to your answer, clears it up. Thank you!</p></pre>lstokeworth: <pre><p>A <a href="https://golang.org/ref/spec#ParameterDecl" rel="nofollow">parameter declaration</a> is:</p> <pre><code>ParameterDecl = [ IdentifierList ] [ &#34;...&#34; ] Type . </code></pre> <p>where the <a href="https://golang.org/ref/spec#Type" rel="nofollow">Type</a> is either the name of the type or a literal:</p> <pre><code>Type = TypeName | TypeLit | &#34;(&#34; Type &#34;)&#34; . </code></pre> <p>The identifiers <code>string</code> and <code>Vertex</code> are both names of types.</p></pre>blahism: <pre><p>The go guide doesn&#39;t jump into OOP patterns with any depth. It just says that adding a receiver to a function creates a method and this method is a function on an instance of an object. Since go doesn&#39;t have classes you create methods on struct types.</p> <p><a href="http://www.golangbootcamp.com/book/methods" rel="nofollow">http://www.golangbootcamp.com/book/methods</a></p></pre>blackcomb-pc: <pre><p>So is <a href="https://stackoverflow.com/questions/29805583/how-can-i-pass-struct-to-function-as-parameter-in-go-lang?answertab=votes#tab-top" rel="nofollow">this</a> heresy or both ways are identical in behind the curtains?</p></pre>

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

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