2018-08-17 14:27:56 531浏览
今天扣丁学堂区块链培训课程主要给大家介绍一下关于golang搭建静态web服务器的实现方法,首先使用过golang语言的程序猿都应该知道,在使用golang开发的时候,我们是不需要诸如iis,apache,nginx,kangle等服务器支持的。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) }