官术网_书友最值得收藏!

Routing in net/http

Even a simple microservice will need the capability to route requests to different handlers dependent on the requested path or method. In Go this is handled by the DefaultServeMux method which is an instance of ServerMux. Earlier in this chapter, we briefly covered that when nil is passed to the handler parameter for the ListenAndServe function then the DefaultServeMux method is used. When we call the http.HandleFunc("/helloworld", helloWorldHandler) package function we are actually just indirectly calling http.DefaultServerMux.HandleFunc(…).

The Go HTTP server does not have a specific router instead any object which implements the http.Handler interface is passed as a top level function to the Listen() function, when a request comes into the server the ServeHTTP method of this handler is called and it is responsible for performing or delegating any work. To facilitate the handling of multiple routes the HTTP package has a special object called ServerMux, which implements the http.Handler interface.

There are two functions to adding handlers to a ServerMux handler:

func HandlerFunc(pattern string, handler func(ResponseWriter, *Request)) 
func Handle(pattern string, handler Handler)

The HandleFunc function is a convenience function that creates a handler who's ServeHTTP method calls an ordinary function with the func(ResponseWriter, *Request) signature that you pass as a parameter.

The Handle function requires that you pass two parameters, the pattern that you would like to register the handler and an object that implements the Handler interface:

type Handler interface { 
ServeHTTP(ResponseWriter, *Request)
}
主站蜘蛛池模板: 达日县| 炎陵县| 内丘县| 龙里县| 湛江市| 和平县| 阳谷县| 兴安县| 长寿区| 镇平县| 柘荣县| 新乐市| 彰化县| 红原县| 福建省| 玉门市| 云霄县| 江达县| 盱眙县| 仁布县| 靖州| 罗定市| 亚东县| 积石山| 武汉市| 哈密市| 土默特左旗| 安宁市| 同心县| 贺兰县| 安康市| 饶阳县| 图们市| 马龙县| 南陵县| 江口县| 延川县| 大理市| 扶绥县| 临湘市| 濮阳市|