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

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.
主站蜘蛛池模板: 新竹市| 香格里拉县| 芒康县| 玉屏| 洪泽县| 图木舒克市| 齐河县| 吴忠市| 新沂市| 西乌珠穆沁旗| 乌兰浩特市| 安康市| 游戏| 宿松县| 连平县| 铜梁县| 富顺县| 余庆县| 中山市| 新泰市| 柘城县| 宁明县| 贵德县| 民和| 武汉市| 平和县| 镶黄旗| 江川县| 瑞丽市| 垫江县| 宁安市| 汝城县| 安庆市| 原平市| 安塞县| 登封市| 台州市| 壶关县| 博罗县| 滦南县| 德保县|