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

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.

主站蜘蛛池模板: 承德县| 竹溪县| 夏津县| 桃园市| 策勒县| 嘉禾县| 阿克陶县| 上蔡县| 名山县| 玛沁县| 锦州市| 永清县| 阆中市| 靖宇县| 囊谦县| 襄樊市| 福州市| 连平县| 乌拉特中旗| 双辽市| 海安县| 镇远县| 凉山| 台中县| 甘泉县| 阿克陶县| 吕梁市| 门头沟区| 霍州市| 江城| 西贡区| 合江县| 舞阳县| 平度市| 林口县| 庄河市| 衡南县| 奎屯市| 同德县| 普格县| 离岛区|