go1.16 新增的embed在各流行Web框架中的应用

发布于2021-02-08 | 3121 阅读 | 0 喜欢 | golang embed

embed 的基本用法

Go embed的使用非常简单,通过//go:embed指令即可,下面我通过一个例子说明:

package main
import (
   "embed"
   "fmt"
)
//go:embed fire
var s string
//go:embed fire
var b []byte
//go:embed templates
var fs embed.FS
func main() {
   fmt.Println(s)
   fmt.Println(string(b))
   data, err := fs.ReadFile("templates/index.tmpl")
   fmt.Println(err,string(data))
}

以上代码中,主要是通过//go:embed指令达到读取文件内容的目的。//go:embed指令后可以是一个文件,也可以是多个文件(空格隔开即可),也可以是一个目录。 其中string[]byte类型都只能匹配一个文件,如果要匹配多个文件或者一个目录,就要使用embed.FS类型。

embed 在http web中的使用

看到embed这个功能,你首先想到的应该是把以前开发Web应用时使用的静态文件、模板文件打包进应用程序中,所以接下来就来看下embed如何在http web中使用。

下面先来看一个使用http托管静态文件的示例

package main
import (
   "embed"
   "net/http"
)
//go:embed static
var static embed.FS
func main() {
   http.ListenAndServe(":8080", http.FileServer(http.FS(static)))
}

看到了吧就是这么简单,就是这么魔幻,几行代码就实现了静态文件的Web托管,并且可以结合embed特性把静态static目录里的内容全部打包到生成的应用程序中,部署非常方便。 以上代码的核心除了//go:embed指令外,还有通过http.FS这个函数,把embed.FS类型的static转换为http.FileServer函数可以识别的http.FileSystem类型。

embed 在模板中的应用

在Go Web的开发中,除了静态文件外,还有Go Template,可以更好的帮助我们渲染Web网页。下面来下看embed是如何被Go 模板使用的。

package main
import (
   "embed"
   "html/template"
   "net/http"
)
//go:embed templates
var tmpl embed.FS
func main() {
   t, _ := template.ParseFS(tmpl, "templates/*.tmpl")
   http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
      t.ExecuteTemplate(rw,"index.tmpl",map[string]string{"title":"Golang Embed 测试"})
   })
   http.ListenAndServe(":8080",nil)
}

从以上示例中可以看到,template包提供了ParseFS函数,可以直接从一个embed.FS中加载模板,然后用于HTTP Web中。模板文件夹的结构如下所示:

templates
└── index.tmpl

Gin静态文件服务

Gin框架中托管一个静态文件服务非常简单,使用Static方法即可,下面看个例子:

package main
import (
   "embed"
   "github.com/gin-gonic/gin"
   "net/http"
)
//go:embed static
var static embed.FS
func main() {
   r:=gin.Default()
   r.StaticFS("/",http.FS(static))
   r.Run(":8080")
}

从以上示例中可以看到,在Gin中使用embed作为静态文件,也是用过http.FS函数转化的。

Gin HTML 模板

同样的,embed也可以用于Gin的HTML模板中,示例如下:

package main
import (
   "embed"
   "github.com/gin-gonic/gin"
   "html/template"
)
//go:embed templates
var tmpl embed.FS
//go:embed static
var static embed.FS
func main() {
   r:=gin.Default()
   t, _ := template.ParseFS(tmpl, "templates/*.tmpl")
   r.SetHTMLTemplate(t)
   r.GET("/", func(ctx *gin.Context) {
      ctx.HTML(200,"index.tmpl",gin.H{"title":"Golang Embed 测试"})
   })
   r.Run(":8080")
}

和前面的模板例子一样,也是通过template.ParseFS函数先加载embed中的模板,然后通过Gin的SetHTMLTemplate设置后就可以使用了。

Fiber静态文件服务

Fiber中要使用embed托管一个静态文件服务,需要使用它的FileSystem,看如下示例。

package main
import (
   "embed"
   "github.com/gofiber/fiber/v2"
   "github.com/gofiber/fiber/v2/middleware/filesystem"
   "net/http"
)
//go:embed templates
var tmpl embed.FS
//go:embed static
var static embed.FS
func main() {
   app := fiber.New()
   app.Use("/", filesystem.New(filesystem.Config{
      Root: http.FS(static),
   }))
   app.Listen(":3000")
}

同样的也是使用http.FS函数,然后结合fiber提供的filesystem达到静态托管文件服务的目的。

运行这段程序,然后在浏览器中输入http://127.0.0.1:3000/static/index.html即可看到效果。

Fiber HTML 模板

搞定了静态文件托管,那么Fiber HTML模板也就比较容易了,代码如下所示:

package main
import (
   "embed"
   "github.com/gofiber/fiber/v2"
   "github.com/gofiber/template/html"
   "net/http"
)
//go:embed templates
var tmpl embed.FS
func main() {
   engine := html.NewFileSystem(http.FS(tmpl), ".tmpl")
   app := fiber.New(fiber.Config{Views: engine})
   app.Get("/", func(ctx *fiber.Ctx) error {
      return ctx.Render("templates/index", fiber.Map{"title": "Golang Embed 测试"})
   })
   app.Listen(":3000")
}

通过html.NewFileSystem函数加载embed,然后把它当做视图渲染器,传给fiber.New即可,现在运行这个段代码,访问http://127.0.0.1:3000就可以看到模板渲染后的效果了。

小结

通过以上介绍,相信你肯定熟练的掌握了在各个框架中如何使用embed的了,其实我们可以总结下,会发现这个http.FS函数用的最多,因为它是一个把embed.FS转为http.FileSystem的工具函数,这样就可以在各个Web框架中直接使用。