<p>Let's search the standard library for how often <code>fallthrough</code> appears:</p>
<pre><code>/usr/lib/go/src$ grep -r switch | wc -l
4066
/usr/lib/go/src$ grep -r "\.\(type\)" | wc -l
275
/usr/lib/go/src$ grep -r "fallthrough" | wc -l
221
</code></pre>
<p>So of 4066 - 275 = 3791 expression <code>switch</code>es, only 221 use <code>fallthrough</code>. That's only 5.8%! To think that in the rest of the C family, this is the <em>default</em> behaviour for only 6% of cases!</p>
<p>Suppose that each of the 3570 <code>switch</code>es that doesn't use <code>fallthrough</code> has 3 cases, two of which would require a <code>break</code>. That's 6 extra characters (including the newline). 3570 × 2 × 6 = 42840 bytes we save by this behaviour. This could easily slow down the lexer or a download by a full microsecond!</p>
<hr/>**评论:**<br/><br/>Artemis2: <pre><p>Do we have a <a href="/r/golangcirclejerk">/r/golangcirclejerk</a> yet?</p></pre>anacrolix: <pre><p>It was merged into <a href="/r/golang">/r/golang</a>.</p></pre>f2f: <pre><p>use this:</p>
<p><a href="https://play.golang.org/p/8x-1AcbaL0" rel="nofollow">https://play.golang.org/p/8x-1AcbaL0</a></p>
<p>some data:</p>
<p><a href="https://docs.google.com/spreadsheets/d/151I--SO2EXIxs_ffnQL9_V_YdpY00N_TfBRVI5EgUFU/edit?usp=sharing" rel="nofollow">https://docs.google.com/spreadsheets/d/151I--SO2EXIxs_ffnQL9_V_YdpY00N_TfBRVI5EgUFU/edit?usp=sharing</a></p></pre>Bake_Jailey: <pre><p>Neat. What about things like default values? I think it might be interesting to see the statistics on how many variables are defined without a value and used (probably harder than a grep into wc), since that's another nicety.</p></pre>el-chupa: <pre><p>Your analysis is flawed; e.g. <code>grep -r switch | grep -v '{'</code>. I'm sure someone's already written a tool to parse the syntax and do a proper count of it but I forget who it was. In either case it's trivial to do it with <code>go/parser</code> etc. so no need to <code>grep</code> for anything.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传