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
Go's HTTP server takes in an address to listen on, and a handler. Internally, it creates a TCP listener to accept connections on the given address, and whenever a request comes in, it:
http.Requesthttp.ResponseWriter for sending the responseServeHTTP method, passing in the Request and ResponseWriterThe handler can be anything that satisfies the Handler interface, which your foo type does:
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
The standard library also includes some conveniences, like HandlerFunc (which allows you to pass any func(ResponseWriter, *Request) and use it as a Handler) and ServeMux, which allows you to register many Handlers and choose which one handles which request based on the incoming request path.