gin返回静态文件 - Go语言中文社区

gin返回静态文件


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")
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首页
}
版权声明:本文来源简书,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.jianshu.com/p/b938cb846b59
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-01-12 12:04:37
  • 阅读 ( 3398 )
  • 分类:Go Web框架

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢