• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
留白
世人慌慌张张,不过图碎银几两
博客园    首页    新随笔       管理    订阅  订阅
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首页
}

  

posted on 2020-08-22 11:55  留白s  阅读(1357)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3