- Go Web Development Cookbook
- Arpit Aggarwal
- 167字
- 2021-08-27 19:01:17
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.
推薦閱讀
- 物聯網與北斗應用
- 連接未來:從古登堡到谷歌的網絡革命
- Mastering Machine Learning for Penetration Testing
- HTML5 Game development with ImpactJS
- 物聯網時代
- 面向物聯網的嵌入式系統開發:基于CC2530和STM32微處理器
- 世界互聯網發展報告·2019
- 區塊鏈輕松上手:原理、源碼、搭建與應用
- Yii Application Development Cookbook(Second Edition)
- 網管員必讀:網絡管理(第2版)
- CCNP TSHOOT(642-832)認證考試指南
- 云計算技術與標準化
- Getting Started with tmux
- Python API Development Fundamentals
- 物聯網