Having trouble understanding functions with multiple returns

polaris · · 679 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello gophers, my programming background is mostly Java and I am having some trouble understanding functions with multiple return values. </p> <pre><code>result = uuid.NewV4().String() </code></pre> <p>This doesn&#39;t work because </p> <pre><code>uuid.NewV4() </code></pre> <p>returns </p> <pre><code>func NewV4() (u *UUID, err error) </code></pre> <p>is there anyway to disregard the error? Or is the &#34;go way&#34; of doing things to use a lot of variables</p> <pre><code>myUUID, _ := uuid.NewV4() result = myUUID.String() </code></pre> <p>Thanks for the help! I know I should be handling the error appropriately, but for the sake of helping me comprehend how these multiple return values work please assume I don&#39;t have to. Thanks! </p> <p>Edit: used myUUID.String() rather then casting to string woops</p> <hr/>**评论:**<br/><br/>Ainar-G: <pre><p>Welcome to Go. The &#34;Go way&#34; is to <strong>never</strong> ignore errors.</p> <pre><code>uuid, err := uuid.NewV4() if err != nil { return &#34;&#34;, err } return string(uuid), nil </code></pre></pre>twek: <pre><p>Thanks! So there is no way to chain another method call off of a method that returns multiple values? (I know I need to always handle errors but what if the method returns two values one of which is not an error?)</p></pre>captncraig: <pre><p>Nope. Go values simplicity over being able to do things on one line. If a function returns multiple values, you are prevented from using it in inline ways like chaining on method calls to single parts. </p></pre>natefinch: <pre><p>The go way is really not to do &#34;fluent&#34; style coding, i.e. foo().bar().baz(). Line returns are not your enemy :)</p></pre>twek: <pre><p>I&#39;m a java engineer. This will take some getting used to. So far I love the language though. Its everything I&#39;ve ever wanted as a Linux developer</p></pre>MoneyWorthington: <pre><p>Another alternative is to wrap it in a function that either returns the value or panics if the error is non-nil, a la <a href="http://golang.org/pkg/text/template/#Must">template.Must()</a>.</p></pre>Ainar-G: <pre><p>You might try something like <a href="https://stackoverflow.com/questions/27229402/is-it-possible-to-get-return-values-selectively-on-single-value-contexts-in-go/27229544#27229544" rel="nofollow">this</a>.</p></pre>hariharan-uno: <pre><p>I don&#39;t think there is a way to disregard the error. Your suggested solution is the right way (to disregard the error) I guess.</p> <p>Also, Don&#39;t disregard the error. You will see its importance, when the program isn&#39;t working as intended and you don&#39;t have a proper error message to know the reason.</p></pre>twek: <pre><p>Thanks for the reply, perhaps you can weigh in on the other question I asked <a href="/u/Ainar-G" rel="nofollow">/u/Ainar-G</a></p></pre>

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

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