静态文件
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
router := gin.Default()
router.Static("/assets", "./assets")
router.StaticFS("/more_static", http.Dir("assets"))
router.StaticFile("/favicon.ico", "./assets/a.png")
router.Run(":8080")
}
在我的项目下创建一个assets文件夹
直接上图就行:
1、访问more_static
2、访问assets下的某个文件
3、直接访问assets下的某个文件
下载服务端文件
我们就下载我们上例中的图片,路径为
http://127.0.0.1:8080/favicon.ico
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
router := gin.Default()
router.StaticFile("/favicon.ico", "./assets/a.png")
router.GET("/someDataFromReader", func(c *gin.Context) {
response, err := http.Get("http://127.0.0.1:8080/favicon.ico")
if err != nil || response.StatusCode != http.StatusOK {
c.Status(http.StatusServiceUnavailable)
return
}
reader := response.Body
contentLength := response.ContentLength
contentType := response.Header.Get("Content-Type")
extraHeaders := map[string]string{
"Content-Disposition": `attachment; filename="gopher.png"`,
}
c.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders)
})
router.Run(":8080")
}
浏览器输入http://127.0.0.1:8080/someDataFromReader 就实现了图片的下载
模版语言(1)
虽然现在业务开发,需要数据分离,但是web框架都有自己的模版语言,比如Django的Jinja2,Jinja2是基于python的模板引擎,功能比较类似于于PHP的smarty,J2ee的Freemarker和velocity。
今天就来了解下gin里面的是如何使用的,这些东西都很简单,直接上代码,看效果即可
func main() {
router := gin.Default()
router.LoadHTMLGlob("template/*")
router.GET("/index", func(c *gin.Context) {
c.HTML(http.StatusOK, "view.html", gin.H{
"title": "Main website",
})
})
router.Run(":8080")
}
看一下view.html代码
<html>
<h1>
{{ .title }}
</h1>
</html>
直接上效果图:
模版语言(2)
上一个例子的模版页面,是放到template的目录下,如果在template下有多级目录呢?比如下面这个截图
然后看后端代码如下:
func main() {
router := gin.Default()
router.LoadHTMLGlob("template/**/*")
router.GET("/posts/index", func(c *gin.Context) {
c.HTML(http.StatusOK, "index1.tmpl", gin.H{
"title": "Posts",
})
})
router.GET("/users/index", func(c *gin.Context) {
c.HTML(http.StatusOK, "users/index2.tmpl", gin.H{
"title": "Users",
})
})
router.Run(":8080")
}
index1.tmpl
<html><h1>
{{ .title }}
</h1>
<p>Using posts/index1.tmpl</p>
</html>
index2.tmpl
{{ define "users/index2.tmpl" }}
<html><h1>
{{ .title }}
</h1>
<p>Using users/index2.tmpl</p>
</html>
{{ end }}
效果图如下:
自定义模版函数
我们还可以自己定制模版函数
You may use custom delims
r := gin.Default()
r.Delims("{[{", "}]}")
r.LoadHTMLGlob("/path/to/templates"))
设置router.Delims
需要的格式,然后SetFuncMap
设置需要的值函数,然后通过c.HTML
渲染数据即可
代码比较简单,我都是直接上代码,看效果即可
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"html/template"
"net/http"
"time"
)
func formatAsDate(t time.Time) string {
year, month, day := t.Date()
return fmt.Sprintf("%d/%02d/%02d", year, month, day)
}
func main() {
router := gin.Default()
router.Delims("{[{", "}]}")
router.SetFuncMap(template.FuncMap{
"formatAsDate": formatAsDate,
})
router.LoadHTMLFiles("template/a.html")
router.GET("/raw", func(c *gin.Context) {
c.HTML(http.StatusOK, "a.html", map[string]interface{}{
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
})
})
router.Run(":8080")
}
模版代码a.html
<body>
Date: {[{.now | formatAsDate}]}
</body>
最后看下效果图: