<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'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'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'm not too sure about Go). The real non-performance reason to prefer it is because then you can modify the struct that's passed in because it's not actually copied and you then operate on the original. If you specifically want to prohibit that possibility inside of you functions, that'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'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 "always" is wrong almost always. "Pointer vs value" is a: It depends.</p></pre>kostix: <pre><p>Oh, please...</p>
<ul>
<li><p>No, quite often, you'd want to pass struct values "as is"--making
copies at the call site. If your struct type contains just a few small
fields, that's absolutely OK, and won't result in performance drops.</p></li>
<li><p>"Interface arguments" are <em>not</em> "passed as pointers".</p>
<p>If a formal argument of a function has interface type, the actual
argument might first get "boxed" (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'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's not what's happening with struct. With struct (as in the example from the Go Tour), you'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's not how all the data types I'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'm probably missing something but just trying to clarify my point of confusion with you. To recap: I've been seeing/expecting:</p>
<pre><code><func>(<name of param> <type>)
</code></pre>
<p>but this example is showing me</p>
<pre><code><func>(<name of param> <name of struct>)
</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
<func>(<name of param> <name of struct>) </p>
</blockquote>
<p>No , it's not name of struct , it is the type Vertex you just defined. It'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'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 ] [ "..." ] 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 | "(" Type ")" .
</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'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'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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传