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

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.

主站蜘蛛池模板: 湖口县| 平原县| 新安县| 富顺县| 苍溪县| 甘南县| 静宁县| 木兰县| 余姚市| 长顺县| 全南县| 富民县| 抚顺市| 禄丰县| 鄂伦春自治旗| 通州区| 故城县| 明溪县| 新竹县| 北宁市| 阳泉市| 方正县| 潜山县| 徐汇区| 开化县| 黄梅县| 竹溪县| 濮阳县| 常熟市| 建昌县| 阜新| 平邑县| 韶关市| 响水县| 太谷县| 玛多县| 邵阳市| 观塘区| 罗甸县| 黄山市| 千阳县|