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

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)
}
主站蜘蛛池模板: 诏安县| 临泽县| 成都市| 乌拉特中旗| 剑阁县| 奉节县| 和平县| 龙岩市| 南宫市| 渝中区| 汤阴县| 磴口县| 临沧市| 安阳县| 扶风县| 郎溪县| 贡觉县| 宜良县| 塘沽区| 烟台市| 九龙坡区| 博湖县| 宁安市| 浠水县| 乡城县| 道真| 闵行区| 韶山市| 城口县| 图木舒克市| 贵港市| 泽普县| 永兴县| 丁青县| 崇左市| 安乡县| 翼城县| 化隆| 彰化市| 广元市| 龙山县|