<p>Because I love Go's <code>switch</code> as an <code>if-else</code> feature, I'm tempted to use it everywhere. But I know it can be less clear than just using <code>if-else</code>. Are there any good clarity guidelines in this respect?</p>
<hr/>**评论:**<br/><br/>packetlust: <pre><p>I won't claim to be a stellar programmer, more of a coder really, but I find myself liking to use "switch {" more than "if else if" chains. I just think it easier to read most of the time. Your mileage may vary</p></pre>Gyrospring: <pre><p>Which one is faster?</p></pre>Partageons: <pre><p>They are exactly the same.</p></pre>barsonme: <pre><p>no, switch statements prevent the function from being inlined (if you compile your code before commit e41f527f4d56a94b33ab73efaae3575760916194) and currently if your switch contains a break statement or you type switch your function will not be inlined. </p></pre>nhooyr: <pre><p>what are all of the rules to ensure a function is inlined?</p></pre>barsonme: <pre><p>Off the top of my head: no loops, selects, switches*, and fewer than N statements. </p>
<p>I can look it up later, it's all documented somewhere. </p></pre>nhooyr: <pre><p>huh really? I've tried multiple times to find the list without any luck.</p></pre>barsonme: <pre><p>From 2013: <a href="https://groups.google.com/d/msg/golang-nuts/V_xI29FGDZM/ubw6lPH2M-MJ" rel="nofollow">https://groups.google.com/d/msg/golang-nuts/V_xI29FGDZM/ubw6lPH2M-MJ</a></p>
<p>switches are allowed now, (per my other comment) but they can't contain <code>break</code> or a type switch. Unsure about the rest.</p>
<p>There's some work on closures: <a href="https://github.com/golang/go/issues/15561" rel="nofollow">https://github.com/golang/go/issues/15561</a></p>
<p>Loops: <a href="https://github.com/golang/go/issues/14768" rel="nofollow">https://github.com/golang/go/issues/14768</a></p></pre>nhooyr: <pre><p>thanks!</p></pre>fripletister: <pre><p>When you're testing a single variable against multiple values.</p></pre>Partageons: <pre><p>You are the second commenter to misunderstand this, somehow. I am not an amateur. I had hoped that by putting "switch {" in the title you would gather that I am <em>not</em> talking about cases where one is comparing a single variable to multiple values, but cases where one is comparing a series of boolean expressions from top to bottom as is typical of if-else statements. I want to know which one should be used when neither <code>if</code> nor <code>switch</code> shines, and what you describe is an obvious case where <code>switch</code> should be used.</p></pre>fripletister: <pre><p>I see what you're after, now. I didn't read carefully enough.</p>
<p>The Golang doc says using <code>switch (true) {</code> in place of <code>if-else…</code> chains is idiomatic, so that seems pretty clear IMO.</p></pre>xienze: <pre><p>It depends on how Go implements switch, but in many C/Java compilers, especially when talking about integer switches a jump table or binary search can be used. Compare that to repeated if/else, where you're guaranteed in worst case to have n comparisons for misses (i.e., what would normally be in the default clause) or whatever value matches the last clause.</p>
<p>The best advice is to use switches because there is a long history of optimizing them; you can't optimize repeated if/elses in the same manner. Again, not sure how Go implements switches, particularly for things like type switches, string switches, etc. but there's a very good chance the compiler emits something optimized.</p></pre>SandalsMan: <pre><p>This is an argument over semantics. If it's your project use it wherever you'd like. If you are contributing to a project with <code>if-else</code> chains all over the codebase then use those.</p></pre>cbarrick: <pre><blockquote>
<p>This is an argument over syntax.</p>
</blockquote>
<p>FTFY</p>
<p>Apologies for being pedantic. Peace.</p></pre>RevMen: <pre><p>I think <code>switch</code> is easier to read and understand than <code>if ... else if ... else</code> and will always choose <code>switch</code> when the two are interchangeable.</p>
<p>The difference between them is you give each the <code>if</code> and each following<code>else if</code> a new and separate condition to evaluate. You're not limited to testing a single value like you are with <code>switch</code>.</p></pre>nsd433: <pre><p>I think the question is about using switch like this, where you can test anything and are not limited to a single value:</p>
<pre><code>var x, y = ...
switch {
case x>y: ...
case x<y: ...
default: ...
}
</code></pre>
<p>to replace the series of if/else if/else you'd write in a language with a less generalized switch.</p></pre>RevMen: <pre><p>Yeah that makes more sense.</p></pre>I_am_toxic_af_x10: <pre><p>the compiler rewrites switches into if-else chains, then further reduces them to branching opcodes for a particular architecture. so it really doesn't matter. It is about options, go gives the programmer a few branching options. use how you see fit.</p></pre>TheMerovius: <pre><p>Personally, I use switch iff I would otherwise need to write "else if". I wouldn't use it only to replace "else".</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传