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

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.
主站蜘蛛池模板: 固安县| 合川市| 盐边县| 金门县| 右玉县| 台江县| 望城县| 婺源县| 普兰店市| 韶关市| 新巴尔虎左旗| 融水| 屏山县| 广昌县| 宜兰市| 江安县| 舟曲县| 松原市| 凤庆县| 江阴市| 达孜县| 衡水市| 平谷区| 呼玛县| 汶上县| 五华县| 凤凰县| 梓潼县| 石河子市| 连平县| 弥渡县| 永登县| 维西| 靖远县| 榆中县| 贡山| 古浪县| 德化县| 陈巴尔虎旗| 铁岭市| 突泉县|