- Go Web Development Cookbook
- Arpit Aggarwal
- 235字
- 2021-08-27 19:01:16
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, but this time the text color has changed from the default black to blue, as shown in the following image:

If we look at the Network tab of Chrome DevTools, we can see main.css, which has been loaded from the static/css directory present on the filesystem.
Let's understand the changes we introduced in the main() method as part of this recipe:
- fileServer := http.FileServer(http.Dir("static")): Here, we created a file server using the FileServer handler of the net/http package, which serves HTTP requests from the static directory present on the filesystem.
- http.Handle("/static/", http.StripPrefix("/static/", fileServer)): Here, we are registering the http.StripPrefix("/static/", fileServer) handler with the /static URL pattern using HandleFunc of the net/http package, which means http.StripPrefix("/static/", fileServer) gets executed and passes (http.ResponseWriter, *http.Request) as a parameter to it whenever we access the HTTP URL with the /static pattern.
- http.StripPrefix("/static/", fileServer): This returns a handler that serves HTTP requests by removing /static from the request URL's path and invokes the file server. StripPrefix handles a request for a path that doesn't begin with a prefix by replying with an HTTP 404.
推薦閱讀
- 黑客攻防實戰技術完全手冊:掃描、嗅探、入侵與防御
- Building E-commerce Sites with VirtueMart Cookbook
- 網絡創新指數研究
- 走進物聯網
- 新一代物聯網架構技術:分層算力網絡
- Building RESTful Web Services with Spring 5(Second Edition)
- Yii Application Development Cookbook(Second Edition)
- 網絡環境中基于用戶視角的信息質量評價研究
- 網絡安全應急響應技術實戰指南
- 物聯網與智能家居
- Android UI Design
- 語音信號處理及Blackfin DSP實現
- 數據血緣分析原理與實踐
- Selenium WebDriver 3 Practical Guide
- 計算機通信網絡安全