1 .// 主页返回静态文件
r.StaticFile("/", "./static/index.html")
// 服务单个静态文件
StaticFile(relativePath, filePath string) IRoutes
// 服务静态文件目录
Static(relativePath, dirRoot string) IRoutes
// 服务虚拟静态文件系统
StaticFS(relativePath string, fs http.FileSystem) IRoutes
r.StaticFile("/", "./static/index.html")
// 服务单个静态文件
StaticFile(relativePath, filePath string) IRoutes
// 服务静态文件目录
Static(relativePath, dirRoot string) IRoutes
// 服务虚拟静态文件系统
StaticFS(relativePath string, fs http.FileSystem) IRoutes
r.StaticFile("/", "./static/index.html") r.StaticFS("/image",http.Dir("./img")) r.Static("image1","./img")
2 .返回首页并添加一个cookie
func sayhelloName(w http.ResponseWriter,r *http.Request){ r.ParseForm() // 解析参数 expiration:=time.Now() expiration=expiration.AddDate(1,0,0) cookie:=http.Cookie{ Name:"golang", Value:"7777", Expires:expiration, } http.SetCookie(w,&cookie) t,_:=template.ParseFiles("index.html") t.Execute(w,t) // 这样就可以直接返回一个index.html首页 }