How does ServeHTTP work?

前端 未结 5 828
温柔的废话
温柔的废话 2020-12-14 03:55

I am studying web development in Golang (Beginner) I came across some code I played around with and I\'m not too sure why it works, I looked through the library source code

5条回答
  •  庸人自扰
    2020-12-14 04:49

    The relevant type in net/http is

    type Handler interface {
        ServeHTTP(ResponseWriter, *Request)
    }
    

    an interface type. Any concrete type implementing this interface can be used to serve HTTP request. Your bar is of type foo and foo implements the Handler interface. If the builtin HTTP server has to handle a request it will call the ServeHTTP method of your bar.

提交回复
热议问题