<p><a href="https://youtu.be/QM1iUe6IofM?t=42m18s" rel="nofollow">https://youtu.be/QM1iUe6IofM?t=42m18s</a></p>
<p>See the video's suggestion at the 42 minute mark. I'd be curious as to what the community would think of such a feature. What really stood out to me in this video was how neatly aligned it was with Go's philosophy.</p>
<p>Copied these thoughts from Go-nuts post:</p>
<p>Is that just syntactic sugar for being able to do one of these two approaches?
<a href="http://play.golang.org/p/79_FwRp_uE" rel="nofollow">http://play.golang.org/p/79_FwRp_uE</a></p>
<p>Justin</p>
<p>This can be written as </p>
<p>a = func(x, y int) int {
...
// return from the use block
return 3
}(x, y) // or as a closure.
-kortschak</p>
<p>I believe it wouldn't be just syntactic sugar, because if I remember from the video, the use block statement excluded any of the upper scope variables other than those declared to be 'used'. Or as he had said it, an 'anonymous function that doesn't see anything of its enclosing scope' other than variables passed in as parameters. Justin demonstrated in the playground that x and y can be used in the anonymous function by nature of scope, without being explicitly declare in the function parameters. </p>
<p>I'm just genuinely interested in opinion on how valid or useful though, I'm not looking for feature creep.
-Paul</p>
<hr/>**评论:**<br/><br/>nate510: <pre><p>I would consider this an antipattern, for the same reason that I would discourage using an anonymous function to achieve the same effect: it is resistant to unit testing. </p>
<p>Consider two scenarios using the variables in the example: </p>
<p>1) Your goal is simple, e.g.: </p>
<pre><code>a = x + y
</code></pre>
<p>In these cases, an expression should be used. There is no need to use any kind of anonymous function construct.</p>
<p>2) Your goal is complex, e.g.: </p>
<pre><code>z, err := ioutil.ReadAll(x)
if err != nil {
panic(err)
}
zInt, err := strconv.Atoi(string(z))
if err != nil {
panic(err)
}
return y + zInt
</code></pre>
<p>In this case, you should name a function that accomplishes the task, so that you can test it, <em>even if you only use it in one place</em>. The "use" construct, like an anonymous function, blocks your ability to write unit tests against a complex operation.</p>
<p>Edit: Formatting.</p></pre>stuffsnout: <pre><p>agreed. I also just don't like the idea of having large block of code even if it is broken up into further blocks.</p></pre>captncraig: <pre><p>The only reason I use anonymous functions is when it is really useful to have the parent's locals scoped into the closure. Without that you may as well make a separate named function.</p></pre>SportingSnow21: <pre><p>Gross. While it is, essentially, an anonymous function defined and called in the same 'use' keyword, the scoping he suggests is a mess. Locally defined, but without access to local scope is just a separate function that's had its code wedged into another function. </p></pre>literallyelvis: <pre><p>Go's CSV library lacks the ability to read in rows as maps or structs.</p></pre>VJCORE: <pre><p>how about default distributed thing , like in erlang ?</p></pre>
Is this something that Go could or should implement? https://youtu.be/QM1iUe6IofM?t=42m18s
xuanbao · · 1287 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传