Decorators in Go, without the code bloat

blov · · 617 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><a href="http://andyrees.github.io/2015/your-code-a-mess-maybe-its-time-to-bring-in-the-decorators/" rel="nofollow">http://andyrees.github.io/2015/your-code-a-mess-maybe-its-time-to-bring-in-the-decorators/</a></p> <hr/>**评论:**<br/><br/>peterbourgon: <pre><p>The GopherCon 2015 talk <a href="https://www.youtube.com/watch?v=xyDkyFjzFVc" rel="nofollow">Embrace the Interface</a> described this pattern and its application extensively.</p></pre>1spudrocket: <pre><p>Yeah, thats a great talk . Another one that echoes the sentiment I was attempting to convey is &#34;dotGo 2014 - Blake Mizerany - Three fallacies of dependencies&#34; <a href="https://www.youtube.com/watch?v=yi5A3cK1LNA" rel="nofollow">https://www.youtube.com/watch?v=yi5A3cK1LNA</a> Basically, think twice before creating a large package, just to find out later that the standard library already covers. :-) Refactor your knowledge of Go as well as your code :-)</p></pre>elithrar_: <pre><p>Functions like these are often called middleware (even when not in a HTTP context). Decorators make me think of Python&#39;s <code>@something</code> annotations above a function declaration.</p> <p>One big tip: a <code>func (http.Handler) http.Handler</code> is far more flexible, because many libraries don&#39;t just deal in the (limited) <code>http.HandlerFunc</code>. </p> <p>Some good reading on HTTP middleware patterns in particular:</p> <ul> <li><a href="https://justinas.org/writing-http-middleware-in-go/" rel="nofollow">https://justinas.org/writing-http-middleware-in-go/</a></li> <li><a href="http://www.alexedwards.net/blog/making-and-using-middleware" rel="nofollow">http://www.alexedwards.net/blog/making-and-using-middleware</a></li> </ul></pre>robertrolandorg: <pre><p>The only condition I&#39;ve heard people call this &#34;middleware&#34; is in the HTTP handler scenario. Everywhere else, this is basically a textbook implementation of the Gang of Four &#34;Decorator&#34; pattern. </p> <p><a href="https://en.wikipedia.org/wiki/Decorator_pattern" rel="nofollow">https://en.wikipedia.org/wiki/Decorator_pattern</a></p> <p>And this is something I&#39;ve found myself doing a few times in Go lately, and not in an HTTP context.</p></pre>

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

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