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

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.

主站蜘蛛池模板: 宁阳县| 新乐市| 临漳县| 黄梅县| 上饶市| 深泽县| 灵山县| 闽清县| 班玛县| 青川县| 虎林市| 镇远县| 嘉兴市| 交口县| 乐业县| 富源县| 玉树县| 微博| 宁阳县| 观塘区| 南华县| 英吉沙县| 绩溪县| 阳信县| 东兴市| 满洲里市| 岳西县| 潍坊市| 上杭县| 普陀区| 涞水县| 上高县| 平阴县| 黄冈市| 泰宁县| 大名县| 城固县| 凤冈县| 合江县| 罗定市| 临泽县|