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

How it works…

Once we run the program, the HTTP server will start locally listening on port 8080.

Browsing http://localhost:8080 will show us the same output we saw in our previous recipe, as shown in the following screenshot:

Let's understand the changes we introduced in the main() method as part of this recipe:

  • router :=mux.NewRouter(): Here we instantiated the gorilla/mux router calling the NewRouter() handler of the mux router.
  • router.HandleFunc("/",renderTemplate).Methods("GET"): Here we registered the / URL pattern with the renderTemplate handler. This means renderTemplate will execute for every request with the URL pattern /.
  • router.PathPrefix("/").Handler(http.StripPrefix("/static", http.FileServer(http.Dir("static/")))): Here we are registering / as a new route along with setting the handler to be executed once it is called.
  • http.StripPrefix("/static", http.FileServer(http.Dir("static/"))): This returns a handler that serves HTTP requests by removing /static from the request URL's path and invoking the file server. StripPrefix handles a request for a path that doesn't begin with a prefix by replying with an HTTP 404.
主站蜘蛛池模板: 紫阳县| 大新县| 瓦房店市| 会同县| 瑞金市| 霍邱县| 周宁县| 徐汇区| 木兰县| 乌海市| 林周县| 繁峙县| 天门市| 个旧市| 临桂县| 宝坻区| 富源县| 荣成市| 黑龙江省| 祥云县| 嵊泗县| 稷山县| 阿勒泰市| 乐都县| 伊春市| 平原县| 定远县| 平果县| 巴塘县| 昂仁县| 惠水县| 龙陵县| 房山区| 抚顺市| 鄂托克前旗| 海晏县| 江城| 武山县| 克东县| 永德县| 南召县|