<pre><code>type Middleware = func(http.Handler) http.Handler
func MustLogin(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w,r)
})
}
</code></pre>
<p>here is my signature and an example middleware. Everything is fine just what i cannot understand is MustLogin is a middleware and supposedly will return a http.Handler but how come compiler doesnt complain that the <code>HandlerFunc</code> is not a <code>http.Handler</code> because it doesnt have a <code>ServeHttp</code> method so it doesn't implement the <code>http.Handler</code> interface</p>
<hr/>**评论:**<br/><br/>anossov: <pre><blockquote>
<p>it doesnt have a ServeHttp method</p>
</blockquote>
<p>Sure it does: <a href="https://golang.org/pkg/net/http/#HandlerFunc.ServeHTTP" rel="nofollow">https://golang.org/pkg/net/http/#HandlerFunc.ServeHTTP</a></p>
<p><a href="https://golang.org/src/net/http/server.go?s=57023:57070#L1904" rel="nofollow">https://golang.org/src/net/http/server.go?s=57023:57070#L1904</a></p></pre>nefthias: <pre><p>oh so that was what i was missing</p></pre>TheMerovius: <pre><p>HandlerFunc does have a <a href="https://godoc.org/net/http#HandlerFunc.ServeHTTP" rel="nofollow">ServeHTTP</a> method.</p></pre>nefthias: <pre><p>yes i figured that right now </p></pre>shovelpost: <pre><blockquote>
<p>but how come compiler doesnt complain that the HandlerFunc is not a http.Handler because it doesnt have a ServeHttp method so it doesn't inherit the http.Handler interface</p>
</blockquote>
<p>Type <a href="https://golang.org/pkg/net/http/#HandlerFunc" rel="nofollow"><code>http.HandlerFunc</code></a> has a <a href="https://golang.org/pkg/net/http/#HandlerFunc.ServeHTTP" rel="nofollow"><code>ServeHTTP</code></a> method.</p>
<p>Also don't forget that interfaces in Go are satisfied implicitly. There's no "inheritance" involved.</p></pre>nefthias: <pre><p>I intended to use the word implement fixing it now</p></pre>
![](https://static.golangjob.cn/static/img/banner.png)