<p>Since tabs vs spaces is not an issue in Go (it is tabs), I'd like to ask a pendantic question: if you think using 'var' for each variable declaration, or one 'var ( )' to group them all is preferable. I'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'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 '=' assignments nicely.</p>
<p>However, single vars on multiple lines, is not the idiomatic way. But hey, it's in the language... so I can use it, right? right?.. I'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 'var' 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're touching existing code, try to be consistent with what's being done. I don't expect this would affect compilation speed to any degree that you'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'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 := ""
i := getValue(123)
</code></pre>
<p>4.</p>
<pre><code>s, i := "", getValue(123)
</code></pre>
<p>I personally like 4th way as it's more compact (where it makes sense obviously).</p></pre>balacode: <pre><p>The 4th way looks better, provided you're not initializing too many vars. It'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 'var' 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'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're mostly not on 80x24 terminal windows any more. Vertical space is still valuable, but it'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's true. I'm just trying to establish the best standard to use in my code and keep to it. Probably will go with ':=' instead of var, mostly.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传