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