'var' style: to group or not to group, that is the question

xuanbao · · 377 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Since tabs vs spaces is not an issue in Go (it is tabs), I&#39;d like to ask a pendantic question: if you think using &#39;var&#39; for each variable declaration, or one &#39;var ( )&#39; to group them all is preferable. I&#39;m declaring each var for two reasons: </p> <p>1) when you do a bulk search (find in files), you can be sure where each variable is initialized by just looking at one search result line.</p> <p>2) It&#39;s 2 lines saved for each group of vars</p> <p>On the other hand, a var block looks cleaner and gofmt aligns all the &#39;=&#39; assignments nicely.</p> <p>However, single vars on multiple lines, is not the idiomatic way. But hey, it&#39;s in the language... so I can use it, right? right?.. I&#39;m not talking of multiple assignment abuse here (a, b, c, d := cat, whale/2, pi<em>7, ratio</em>2+8) but single vars vs. var blocks.</p> <p>The real important question for those who understand the Go compiler is: does using individual &#39;var&#39; declarations vs var blocks make compilation any slower? Please share your thoughts.</p> <hr/>**评论:**<br/><br/>sh41: <pre><p>Either way is fine. If you&#39;re touching existing code, try to be consistent with what&#39;s being done. I don&#39;t expect this would affect compilation speed to any degree that you&#39;d be able to notice.</p> <p>I typically start with individual var lines, but sometimes group multiple var lines into a single var () block when I want to emphasize a little that they&#39;re all logically related.</p></pre>hipone: <pre><p>I think there are more ways to declare variables (function-scope), that are actually used in Go projects.</p> <p>1.</p> <pre><code>var s string var i = getValue(123) </code></pre> <p>2.</p> <pre><code>var ( s string i = getValue(123) ) </code></pre> <p>3.</p> <pre><code>s := &#34;&#34; i := getValue(123) </code></pre> <p>4.</p> <pre><code>s, i := &#34;&#34;, getValue(123) </code></pre> <p>I personally like 4th way as it&#39;s more compact (where it makes sense obviously).</p></pre>balacode: <pre><p>The 4th way looks better, provided you&#39;re not initializing too many vars. It&#39;s good when you have something like x, y := 0, 17 for coordinates but when misused can lead to harder to read code. E.g. trying to initialize 4 or 5 vars on the same line. Also very useful when you need to flip two variables: x, y = y, x</p></pre>peterbourgon: <pre><p>Almost any time I have more than one line with a repeated prefix declaration that can be grouped, I group them. So never</p> <pre><code>var s string var i = getValue(123) </code></pre> <p>and always</p> <pre><code>var ( s string i = getValue(123) ) </code></pre> <p>I dunno, totally personal preference, but I think it looks a lot nicer.</p> <p>—</p> <blockquote> <p>does using individual &#39;var&#39; declarations vs var blocks make compilation any slower?</p> </blockquote> <p>If this is true I will be very surprised.</p></pre>SeerUD: <pre><p>I&#39;m not a big fan of grouping them up. I prefer to just repeat the keyword at the start. For the <code>type</code> keyword I find this also makes it easier to <code>grep</code> for things. It uses less lines not grouped up too, and I think the indentation looks nicer.</p></pre>fubo: <pre><p>Something to keep in mind is that we&#39;re mostly not on 80x24 terminal windows any more. Vertical space is still valuable, but it&#39;s not <em>as</em> valuable as it once was.</p></pre>balacode: <pre><p>80x24 terminal, lol. Space is not such a big issue, that&#39;s true. I&#39;m just trying to establish the best standard to use in my code and keep to it. Probably will go with &#39;:=&#39; instead of var, mostly.</p></pre>

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

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