What's the difference between http.HandleFunc and http.HandlerFunc?

polaris · · 378 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m reading two articles in which one is using <strong>http.HandleFunc</strong> and the other <strong>http.HandlerFunc</strong> (handle vs handler). Both functions seem to be used in the same way, so I can&#39;t tell how they are different.</p> <p><a href="https://medium.com/@matryer/context-has-arrived-per-request-state-in-go-1-7-4d095be83bd8#.mqqap3s7s" rel="nofollow">Finally some context</a>:</p> <pre><code>func WithAuth(a Authorizer, next http.Handler) http.Handler { return http.HandleFunc(func(w http.ResponseWriter, r *http.Request) { next.ServeHTTP(w, r.WithContext(ctx)) }) } </code></pre> <p>and <a href="http://www.alexedwards.net/blog/making-and-using-middleware" rel="nofollow">Using middleware</a>:</p> <pre><code>func middlewareOne(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { next.ServeHTTP(w, r) }) } </code></pre> <p>Looking at the <em>net/http</em> package, it looks like <strong>http.HandlerFunc</strong> is a type with a different signature than used above.</p> <p><a href="https://golang.org/src/net/http/server.go?s=51035:51082#L1712" rel="nofollow">type HandlerFunc func(ResponseWriter, *Request)</a></p> <p>I&#39;m confused as to what/how <strong>http.HandlerFunc</strong> is even executing. Am I looking at the wrong documentation? Can anyone help make sense of this?</p> <hr/>**评论:**<br/><br/>TheMerovius: <pre><p><code>http.HandlerFunc</code> is a helper type, to make a function with the same signature as <code>http.Handler.ServeHTTP</code> into an <code>http.Handler</code>. <code>http.HandleFunc</code>, on the other hand, is a shorthand to use such a function and register it as a Handler (like <code>http.Handle</code> does for a <code>Handler</code>). So the following to are <a href="https://golang.org/src/net/http/server.go#L2076" rel="nofollow">equivalent</a>: <code>http.Handle(&#34;/&#34;, http.HandlerFunc(f))</code> and <code>http.HandleFunc(&#34;/&#34;, f)</code>, if <code>f</code> is of type <code>func(http.ResponseWriter, *http.Response)</code>.</p> <p>Arguably, there&#39;s some confusing duplication here.</p> <p>Basically:</p> <ul> <li>Both are useful if you have a function with the correct signature and want to use it as a handler</li> <li><code>HandleFunc</code> is a convenient shortcut if you want to register that function on the <code>DefaultServeMux</code></li> <li><code>HandlerFunc</code> is needed, if you actually need an <code>http.Handler</code>, e.g. to pass it to some middleware.</li> </ul></pre>metamatic: <pre><p>The way I (try to) remember it is: </p> <ul> <li><code>HandleFunc</code> lets you Handle a request with a Func, whereas</li> <li><code>HandlerFunc</code>makes a Handler from a Func.</li> </ul> <p>Neither statement then makes sense if you get Handle/Handler swapped in your mind. Handle (verb) vs Handler (noun).</p></pre>ultra_brite: <pre><p>The first article(1) likely has an error, obviously <strong>http.HandleFunc DOESNT return anything and takes 2 arguments</strong>(2).</p> <p>1:<a href="https://medium.com/@matryer/context-has-arrived-per-request-state-in-go-1-7-4d095be83bd8#.mqqap3s7s" rel="nofollow">https://medium.com/@matryer/context-has-arrived-per-request-state-in-go-1-7-4d095be83bd8#.mqqap3s7s</a></p> <p>2:<a href="https://godoc.org/net/http#HandleFunc" rel="nofollow">https://godoc.org/net/http#HandleFunc</a></p></pre>Integralist: <pre><p>Interestingly I wrote a blog post about this a while back when covering Go&#39;s func Type <a href="http://www.integralist.co.uk/posts/golang-webserver.html" rel="nofollow">http://www.integralist.co.uk/posts/golang-webserver.html</a></p></pre>

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

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