Idiomatic parameter order ?

polaris · · 401 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I couldn&#39;t find any documentation on the idiomatic order of parameters. Looking at the stdLib I see that output parameters come first (e.g. copy, serveHTTP).</p> <p>My function has an io.Writer parameter and some others with the data to output using the io.Writer. Should the io.Writer parameter come first ?</p> <hr/>**评论:**<br/><br/>shovelpost: <pre><p>The &#34;idiomatic&#34; way is to follow the variable assignment mnemonic:</p> <pre><code>b := a copy(b, a) foo(b, a) </code></pre> <p>In all those cases you &#34;move&#34; the contents of <code>a</code>to <code>b</code>. So in short, yes, put <code>io.Writer</code> first.</p></pre>mwholt: <pre><p>Never saw it this way, makes sense!</p></pre>AnAge_OldProb: <pre><p>The only rule I know is that context always goes first.</p></pre>ChristophBerger: <pre><p>And for return parameters, <code>error</code>always comes last.</p></pre>The_Sly_Marbo: <pre><p>Good question. I&#39;d say there isn&#39;t much idiom here, but I would probably put the <code>io.Writer</code> first.</p></pre>nhooyr: <pre><p>Personally, I prefer input first. It makes more sense imo given you first get input and then make output.</p></pre>robe_and_wizard_hat: <pre><p>Yeah I always have thought this as well, but traditionally in computering the destination goes before the input.</p></pre>

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

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