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

ServeMux – a basic router in Go

ServeMux is an HTTP request multiplexer. The HandleFunc we used in the preceding section is actually a method of ServeMux. By using ServeMux, we can handle multiple routes. We can also create our own multiplexer. A multiplexer handles the logic of separating routes with a function called ServeHTTP. So, if we create a Go struct with the ServeHTTP method, it can do the job as the in-built multiplexer.

Consider a route as a key in a Go dictionary (map) and a multiplexer as its value. Go finds the multiplexer from the route and tries to execute the ServeHTTP function. In the following section, we will see the usage of ServeMux by creating an API that generates UUID strings.

主站蜘蛛池模板: 瑞安市| 广平县| 涿州市| 奇台县| 马尔康县| 抚松县| 晋宁县| 霍山县| 永川市| 沁水县| 新昌县| 达孜县| 绥化市| 赤峰市| 崇阳县| 铜鼓县| 马鞍山市| 镇巴县| 工布江达县| 民和| 富顺县| 九龙县| 平昌县| 平安县| 五台县| 镇安县| 梓潼县| 林州市| 武乡县| 平塘县| 伊春市| 宝兴县| 林口县| 博乐市| 抚州市| 两当县| 永平县| 遵义市| 磐石市| 周宁县| 南投市|