[2018-09-30 10:22:55 最后更新]
https://www.cnblogs.com/yjf512/archive/2012/09/03/2668384.html
在net/http包中,动态文件的路由和静态文件的路由是分开的,动态文件使用http.HandleFunc进行设置,静态文件就需要使用到http.FileServer
package main
import (
"net/http"
)
func main() {
http.Handle("/css/", http.FileServer(http.Dir("template")))
http.Handle("/js/", http.FileServer(http.Dir("template")))
http.HandleFunc("/admin/", adminHandler)
http.HandleFunc("/login/",loginHandler)
http.HandleFunc("/ajax/",ajaxHandler)
http.HandleFunc("/",NotFoundHandler)
http.ListenAndServe(":8888", nil)
}
这里的http.FileServer(http.Dir("template"))的路径是怎么算的要注意下了。相对路径都是从当前执行路径路径下开始算的,这里设置的路径树是这样: