2018-08-17 14:27:56 1336瀏覽
今天扣丁學堂區(qū)塊鏈培訓課程主要給大家介紹一下關(guān)于golang搭建靜態(tài)web服務(wù)器的實現(xiàn)方法,首先使用過golang語言的程序猿都應該知道,在使用golang開發(fā)的時候,我們是不需要諸如iis,apache,nginx,kangle等服務(wù)器支持的。func main() { http.Handle("/css/", http.FileServer(http.Dir("template"))) http.Handle("/js/", http.FileServer(http.Dir("template"))) http.ListenAndServe(":8080", nil) }
src |--main | |-main.go |--template | |-css | |--admin.css | |-js | |--admin.js | |-html | |--404.html
2018/08/17 11:09:28 open template/html/404.html: The system cannot find the path specified.
log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))
// To serve a directory on disk (/tmp) under an alternate URL // path (/tmpfiles/), use StripPrefix to modify the request // URL's path before the FileServer sees it: http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
func Template_dir() string { template_dir := "E:\\project\\gotest\\src\\template" return template_dir } func main() { http.Handle("/css/", http.FileServer(http.Dir(Template_dir()))) http.Handle("/js/", http.FileServer(http.Dir(Template_dir()))) http.ListenAndServe(":8080", nil) }