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首页
}

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM