简介


本文接着上文(Golang GinWeb框架6-绑定请求字符串/URI/请求头/复选框/表单类型)继续探索GinWeb框架

静态文件服务

package mainimport (  "github.com/gin-gonic/gin"  "log"  "net/http"  "os")func main() {  router := gin.Default()  cwd, _ := os.Getwd()  //获取当前文件目录  log.Printf("当前项目路径:%s", cwd)  router.Static("/static", cwd) //提供静态文件服务器, 第一个参数为相对路径,第二个参数为根路径, 这个路径一般放置css,js,fonts等静态文件,前端html中采用/static/js/xxx或/static/css/xxx等相对路径的方式引用  router.StaticFS("/more_static", http.Dir("./")) //将本地文件树结构映射到前端, 通过浏览器可以访问本地文件系统, 模拟访问:http://localhost:8080/more_static  router.StaticFile("/logo.png", "./resources/logo.png")  //StaticFile提供单静态单文件服务, 模拟访问:http://localhost:8080/log.png  // Listen and serve on 0.0.0.0:8080  router.Run(":8080")}

返回文件数据

package mainimport (  "github.com/gin-contrib/cors"  "github.com/gin-gonic/gin"  "net/http")func main() {  router := gin.Default()  router.Use(cors.Default())  router.GET("/local/file", func(c *gin.Context) {    c.File("./main.go")  })  // A FileSystem implements access to a collection of named files.  // The elements in a file path are separated by slash ('/', U+002F)  // characters, regardless of host operating system convention.  // FileSystem接口, 要求实现文件的访问的方法, 提供文件访问服务根路径的HTTP处理器  var fs http.FileSystem = http.Dir("./")  //将本地目录作为文件服务根路径  router.GET("/fs/file", func(c *gin.Context) {    c.FileFromFS("main.go", fs)  //将文件服务系统下的文件数据返回  })  router.Run(":8080")}/*模拟访问文件数据:curl http://localhost:8080/local/file模拟访问文件系统下的文件数据:curl http://localhost:8080/fs/file*/

用文件读出器提供文件数据服务

package mainimport (  "github.com/gin-gonic/gin"  "net/http")func main() {  router := gin.Default()  router.GET("/someDataFromReader", func(c *gin.Context) {    response, err := http.Get("https://raw.githubusercontent.com/gin-gonic/logo/master/color.png")    if err != nil || response.StatusCode != http.StatusOK {  //请求链接中的文件出现错误时, 直接返回服务不可用      c.Status(http.StatusServiceUnavailable)      return    }    reader := response.Body  //用响应体内容构造一个文件读出器    defer reader.Close()    contentLength := response.ContentLength    contentType := response.Header.Get("Content-Type")    extraHeaders := map[string]string{      "Content-Disposition": `attachment; filename="gopher.png"`,    }    // DataFromReader writes the specified reader into the body stream and updates the HTTP code.    // func (c *Context) DataFromReader(code int, contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string) {}    // DataFromReader方法将指定的读出器reader中的内容, 写入http响应体流中, 并更新响应码, 响应头信息等    c.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders)  })  router.Run(":8080")}/*模拟访问:curl http://localhost:8080/someDataFromReader*/

HTML渲染


使用LoadHTMLGlob()方法或LoadHTMLFiles()方法

package mainimport (  "github.com/gin-gonic/gin"  "net/http")func main() {  router := gin.Default()  //LoadHTMLGlob方法以glob模式加载匹配的HTML文件, 并与HTML渲染器结合  router.LoadHTMLGlob("templates/*")  //router.LoadHTMLFiles("templates/template1.html", "templates/template2.html")  router.GET("/index", func(c *gin.Context) {    //HTML方法设置响应码, 模板文件名, 渲染替换模板中的值, 设置响应内容类型Content-Type "text/html"    c.HTML(http.StatusOK, "index.tmpl", gin.H{      "title": "Main website",    })  })  router.Run(":8080")}/*模拟测试:curl http://localhost:8080/index*/

增加模板文件, templates/index.tmpl

<html>  <h1>    {{ .title }}  h1>html>

使用不同文件夹下的相同文件名的模板文件

func main() {  router := gin.Default()  router.LoadHTMLGlob("templates/**/*")  router.GET("/posts/index", func(c *gin.Context) {    c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{      "title": "Posts",    })  })  router.GET("/users/index", func(c *gin.Context) {    c.HTML(http.StatusOK, "users/index.tmpl", gin.H{      "title": "Users",    })  })  router.Run(":8080")}

posts目录下添加模板文件, templates/posts/index.tmpl

{{ define "posts/index.tmpl" }}<html><h1>  {{ .title }}h1><p>Using posts/index.tmplp>html>{{ end }}

users目录下添加模板文件, templates/users/index.tmpl

{{ define "users/index.tmpl" }}<html><h1>  {{ .title }}h1><p>Using users/index.tmplp>html>{{ end }}

自定义模板渲染器


你也可以使用你自定义的HTML模板渲染器, 需要自定义模板文件file1, file2等

package mainimport (  "github.com/gin-gonic/gin"  "html/template"  "net/http")func main() {  router := gin.Default()  //template.ParseFiles(文件1,文件2...)创建一个模板对象, 然后解析一组模板,使用文件名作为模板的名字  // Must方法将模板和错误进行包裹, 返回模板的内存地址 一般用于变量初始化,比如:var t = template.Must(template.New("name").Parse("html"))  html := template.Must(template.ParseFiles("file1", "file2"))  router.SetHTMLTemplate(html) //关联模板和HTML渲染器  router.GET("/index", func(c *gin.Context) {    //HTML方法设置响应码, 模板文件名, 渲染替换模板中的值, 设置响应内容类型Content-Type "text/html"    c.HTML(http.StatusOK, "file1", gin.H{      "title": "Main website",    })  })  router.Run(":8080")}

自定义分隔符


你可以自定义分隔符, 模板中默认的分隔符是{{  }}, 我们也可以修改, 比如下面增加一对中括号

  r := gin.Default()  r.Delims("{[{", "}]}")  r.LoadHTMLGlob("/path/to/templates")

自定义模板方法


详见 示例代码.

模板中与后端都定义好模板方法, 模板渲染时执行该方法, 类似过滤器方法, 比如时间格式化操作

package mainimport (  "fmt"  "html/template"  "net/http"  "time"  "github.com/gin-gonic/gin")func formatAsDate(t time.Time) string {  year, month, day := t.Date()  //Date方法返回年,月,日  return fmt.Sprintf("%d%02d/%02d", year, month, day)  //格式化时间}func main() {  router := gin.Default()  router.Delims("{[{", "}]}") //自定义模板中的左右分隔符  //SetFuncMap方法用给定的template.FuncMap设置到Gin引擎上, 后面模板渲染时会调用同名方法  //FuncMap是一个map,键名关联方法名, 键值关联方法, 每个方法必须返回一个值, 或者返回两个值,其中第二个是error类型  router.SetFuncMap(template.FuncMap{    "formatAsDate": formatAsDate,  })  router.LoadHTMLFiles("./testdata/template/raw.tmpl") //加载单个模板文件并与HTML渲染器关联  router.GET("/raw", func(c *gin.Context) {    c.HTML(http.StatusOK, "raw.tmpl", gin.H{      "now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),    })  })  router.Run(":8080")}/*模拟测试:curl http://localhost:8080/raw*/

定义模板文件: raw.tmpl

Date: {[{.now | formatAsDate}]}

时间格式化结果:

Date: 2017/07/01

多个模板


Gin默认只使用一个html.Template模板引擎, 也可以参考多模板渲染器使用类似Go1.6的块级模板block template功能.

模板相关详情请参考官方template包

参考文档


Gin官方仓库:https://github.com/gin-gonic/gin


END已结束

欢迎大家留言, 订阅, 交流哦!


往期回顾


Golang GinWeb框架6-XML/JSON/YAML/ProtoBuf等渲染

Golang GinWeb框架5-绑定请求字符串/URI/请求头/复选框/表单类型

Golang GinWeb框架4-请求参数绑定和验证

Golang GinWeb框架3-自定义日志格式和输出方式/启禁日志颜色

Golang GinWeb框架2-文件上传/程序panic崩溃后自定义处理方式

Golang GinWeb框架-快速入门/参数解析

Golang与亚马逊对象存储服务AmazonS3快速入门

Golang+Vue实现Websocket全双工通信入门

GolangWeb编程之控制器方法HandlerFunc与中间件Middleware

Golang连接MySQL执行查询并解析-告别结构体

Golang的一种发布订阅模式实现

Golang 并发数据冲突检测器(Data Race Detector)与并发安全

Golang"驱动"MongoDB-快速入门("快码加鞭")

png文件头_Golang GinWeb框架7静态文件/模板渲染相关推荐

  1. 分享:bbed修改数据文件头推进scn与其他数据文件相同

    2019独角兽企业重金招聘Python工程师标准>>> 场景简介: 物理copy表空间数据文件,数据库发生完全检查点,删除该表空间下的数据文件,使该表空间数据文件头的scn与其他数据 ...

  2. 修改oracle的表空间文件scn,分享:bbed修改数据文件头推进scn与其他数据文件相同...

    场景简介: 物理copy表空间数据文件,数据库发生完全检查点,删除该表空间下的数据文件,使该表空间数据文件头的scn与其他数据文件不一致. 场景构造: 1.创建测试表空间 SYS@orser> ...

  3. c++ post请求_Golang GinWeb框架5绑定请求字符串/URI/请求头/复选框/表单类型

    简介 本文接着上文(Golang GinWeb框架4-请求参数绑定和验证)继续探索GinWeb框架 只绑定查询字符串 使用SholdBindQuery方法只绑定查询参数, 而不会绑定post的数据. ...

  4. 来玩Play框架07 静态文件

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Play框架的主要功能是提供动态响应的内容.但一个网络项目中必然有大量的静态内容, ...

  5. c语言二fseek从文件头移动_编程C语言文件的随机读写

    实现随机读写的关键是要按要求移动位置指针,这称为文件的定位. 文件定位 移动文件内部位置指针的函数主要有两个,即rewind()和fseek().rewind函数前面已多次使用过,其调用形式为:    ...

  6. python bottle框架 重定向_Python的web框架bottle静态文件的路径

    这几天想用bottle来做一个简单的基于web页面的小应用,在调用显示静态文件时被路径卡了半天,现在把问题和解决办法写出来备用和分享给有需要的人. 先上代码: from bottle import s ...

  7. python sanic视频_Python Web框架Sanic 静态文件

    我们在写web app(网站)的时候会用到很多静态文件,比如css,JavaScript,图片等,这些文件及其文件夹可以通过 app.static() 方法注册,从而被访问到.该方法有两个必需参数,节 ...

  8. flask 配置静态文件模板文件

    app = Flask(__name__) 参数 描述 import_name 决定flask的根目录,传入__name__,既当前运行的模块 static_url_path 静态文件访问路径,默认/ ...

  9. 静态html如何写入文件,静态HTML模板渲染

    1.模板配置 在 setting.py 中配置 TEMPLATES BACKEND 解析HTML静态文件的模板引擎类型 DIRS 一个文件夹目录的列表,如果设置,则 django 会在这里指定的目录中 ...

最新文章

  1. 2022-2028年中国电梯行业市场调查及前瞻分析报告
  2. 讲讲 Redis 缓存更新一致性
  3. CodeForces 625A Guest From the Past
  4. struts2的OGNL表达式(三)
  5. ​年底大会火爆,看“瑶台”如何搭建一场高质量沉浸式大会
  6. jquery是库还是框架?
  7. LeetCode之Move Zeroes
  8. 在Window上使用Jenkins自动部署和上传快照Java工件
  9. LightGBM中GBDT的实现
  10. 面试题目_数据分析SQL面试题目9套汇总
  11. 在ASP.NET中调用存储过程方法新解
  12. linux java.policy_Linux部署Java环境
  13. SQL条件!=null查不出数据
  14. [转载]资深程序员点评当前某些对Lotus Domino 的不实评论
  15. Runtime.exec使用错误导致延迟.md
  16. C++ printf输出
  17. 深入理解JVM虚拟机(一):JVM运行时数据区
  18. 简历模板下载word格式 空白word简历模板下载 电子版个人简历下载
  19. C++Primer基础部分
  20. hahabet05-com:终于有人把云计算,大数据,人工智能讲明白了--哈哈电竞

热门文章

  1. LINQ to SQL的不足
  2. jquery的选择器之-表单对象属性过滤选择器
  3. Resharper F12下载dll源码
  4. 阿里云新手必踩坑系列 - 安全组
  5. 腾讯 AlloyTeam 正式发布 Canvas 魔幻线条 - curvejs
  6. CSS浏览器兼容问题
  7. Symfony2Book04:Doctrine01-介绍模型(Model)
  8. MakeDAO 推出新漏洞奖励计划,最高赏金1000万美元
  9. GitLab Elasticsearch 私密群组数据泄露 bug 值3000美元
  10. 智慧城市数据采集的四大难点分析及解决措施