Can someone explain this code?

polaris · · 491 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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 &amp;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&#39;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> &#34;feature&#34; 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&#39;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&#39;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&#39;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 &#34;better&#34;? </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&#39;t need to change them all if you change the function&#39;s return values since it will just say &#34;return&#34;.</p> </blockquote></pre>tmornini: <pre><p>I get those &#34;benefits&#34; but don&#39;t agree they&#39;re net-helpful.</p> <p>There&#39;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&#39;s obvious what&#39;s going on, but if it returns <code>(int, int)</code>, it might not be. I&#39;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&#39;t do that, put those ints in a struct where they belong. :-)</p></pre>Bake_Jailey: <pre><p>Not sure that&#39;s always the &#34;right&#34; thing to do. I can think of numerous reasons you wouldn&#39;t really want a struct, especially when you want to discard values you don&#39;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&#39;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&#39;s in a world where most languages have thousands.</p></pre>tmornini: <pre><p>Fair enough.</p> <p>Perhaps I should have said it&#39;s worst feature. :-)</p></pre>

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

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