<p>I was looking over this code <a href="https://github.com/nu7hatch/gouuid/blob/master/uuid.go#L99" rel="nofollow">https://github.com/nu7hatch/gouuid/blob/master/uuid.go#L99</a> which should generate a new UUID v4.
Looking at the <code>NewV4()</code> function it should return a pointer to a UUID right? (which is a slice of bytes) But the function has just <code>return</code> and not <code>return u</code>; or <code>return &u;</code> How does that work?</p>
<hr/>**评论:**<br/><br/>eugene-d: <pre><p><a href="https://tour.golang.org/basics/7" rel="nofollow">https://tour.golang.org/basics/7</a></p></pre>daniels0xff: <pre><p>Thanks.</p></pre>TheBeard_: <pre><p>It's because it uses named values in the return definition. So it know what values to return, by them being set in the method. </p></pre>daniels0xff: <pre><p>Now makes sense :D Thanks.</p></pre>tmornini: <pre><p>As others have pointed out, because it uses what is, IMNSHO, the <em>only bad</em> "feature" of Go, named returns.</p>
<p>For a language that prides itself on being minimal, how this bit of sorcery made it into the 1.0 spec is beyond my ability to comprehend. :-)</p>
<p>I guess it simply goes to show that <em>nobody</em> bats 1,000. :-)</p>
<p>Perhaps it could be removed in 2.0? How do others feel about it?</p>
<p>P.S. I love Go, and I appreciate the incredible work done by others and given to me for free.</p></pre>shovelpost: <pre><p>Agreed.</p>
<p>From what I've heard most people agree that it should be one of the first features to be removed from the language.</p>
<p>Personally I do not mind it too much and what I'd much rather get is a way to do this:</p>
<p><code>return _, err</code> instead of <code>return struct{}, err</code></p>
<p>In other words <code>_</code> should return the zero value of a type.</p></pre>tmornini: <pre><p>I'd make that trade any day!</p></pre>JackOhBlades: <pre><p>Here here. Named returns I find quite unreadable and inconsistent. Can anyone give a scenario in which named returns are "better"? </p>
<p>Edit: found some decent reasons on <a href="http://stackoverflow.com/questions/15089726/why-should-return-parameters-be-named" rel="nofollow">stackoverflow</a></p>
<blockquote>
<p>There are some benefits to naming them:
It serves as documentation. They are auto-declared and initialized to the zero values. If you have multiple return sites, you don't need to change them all if you change the function's return values since it will just say "return".</p>
</blockquote></pre>tmornini: <pre><p>I get those "benefits" but don't agree they're net-helpful.</p>
<p>There's always a struggle between convenience and simplicity...</p></pre>Bake_Jailey: <pre><p>I definitely agree with using them as documentation. If your function returns <code>(int, error)</code>, it's obvious what's going on, but if it returns <code>(int, int)</code>, it might not be. I'd be happy if the names were there only for documentation, and not be actual defined variables, if anything.</p></pre>ChristophBerger: <pre><p>Agreed, using named return values for documentation is fine. Naked return statements, however, are only misleading and should be avoided. What I mean is:</p>
<pre><code>func f() (min int, max int, err error) { // name the return values
// lots of code
return min, max err // always list the return values, never use a naked return
}
</code></pre>
<p>Best of both worlds.</p></pre>tmornini: <pre><p>Don't do that, put those ints in a struct where they belong. :-)</p></pre>Bake_Jailey: <pre><p>Not sure that's always the "right" thing to do. I can think of numerous reasons you wouldn't really want a struct, especially when you want to discard values you don't need. For example, named returns are used like this fairly often in the math library, like with <a href="https://golang.org/pkg/math/#Sincos" rel="nofollow">Sincos</a> or <a href="https://golang.org/pkg/math/#Frexp" rel="nofollow">Frexp</a>.</p></pre>tmornini: <pre><p>What is the disadvantage of a struct that would make you not want one?</p></pre>joushou: <pre><p>Well, it's not actually named returns, but <em>naked</em> returns. Named returns could in theory make sense, but <em>naked</em> returns make things a million times worse.</p>
<p>Not the only bad feature either, though. Go has a few nasty warts, but that's in a world where most languages have thousands.</p></pre>tmornini: <pre><p>Fair enough.</p>
<p>Perhaps I should have said it's worst feature. :-)</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传