什么是 Go embed

在前几天刚发布的Golang 1.16版本中,新增了一个大家期待已久的特性//go:embed,它的作用就是可以在Go语言应用程序中包含任何文件、目录的内容,也就是说我们可以把文件以及目录中的内容都打包到生成的Go语言应用程序中了,部署的时候,直接扔一个二进制文件就可以了,不用再包含一些静态文件了,因为它们已经被打包到生成的应用程序中了。

embed 的基本用法

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

  1. package main
  2. import (
  3. "embed"
  4. "fmt"
  5. )
  6. //go:embed fire
  7. var s string
  8. //go:embed fire
  9. var b []byte
  10. //go:embed templates
  11. var fs embed.FS
  12. func main() {
  13. fmt.Println(s)
  14. fmt.Println(string(b))
  15. data, err := fs.ReadFile("templates/index.tmpl")
  16. fmt.Println(err,string(data))
  17. }

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

embed 在http web中的使用

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

  1. package main
  2. import (
  3. "embed"
  4. "net/http"
  5. )
  6. //go:embed static
  7. var static embed.FS
  8. func main() {
  9. http.ListenAndServe(":8080", http.FileServer(http.FS(static)))
  10. }

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

embed 在模板中的应用

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

  1. package main
  2. import (
  3. "embed"
  4. "html/template"
  5. "net/http"
  6. )
  7. //go:embed templates
  8. var tmpl embed.FS
  9. func main() {
  10. t, _ := template.ParseFS(tmpl, "templates/*.tmpl")
  11. http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
  12. t.ExecuteTemplate(rw,"index.tmpl",map[string]string{"title":"Golang Embed 测试"})
  13. })
  14. http.ListenAndServe(":8080",nil)
  15. }

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

  1. templates
  2. └── index.tmpl

Gin 框架

Gin是一个非常流行的框架,它对于静态文件以及HTML模板支持的也非常好,现在我们来看下它和embed如何结合使用。

Gin静态文件服务

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

  1. package main
  2. import (
  3. "embed"
  4. "github.com/gin-gonic/gin"
  5. "net/http"
  6. )
  7. //go:embed static
  8. var static embed.FS
  9. func main() {
  10. r:=gin.Default()
  11. r.StaticFS("/",http.FS(static))
  12. r.Run(":8080")
  13. }

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

Gin HTML 模板

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

  1. package main
  2. import (
  3. "embed"
  4. "github.com/gin-gonic/gin"
  5. "html/template"
  6. )
  7. //go:embed templates
  8. var tmpl embed.FS
  9. //go:embed static
  10. var static embed.FS
  11. func main() {
  12. r:=gin.Default()
  13. t, _ := template.ParseFS(tmpl, "templates/*.tmpl")
  14. r.SetHTMLTemplate(t)
  15. r.GET("/", func(ctx *gin.Context) {
  16. ctx.HTML(200,"index.tmpl",gin.H{"title":"Golang Embed 测试"})
  17. })
  18. r.Run(":8080")
  19. }

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

Fiber

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

  1. package main
  2. import (
  3. "embed"
  4. "github.com/gofiber/fiber/v2"
  5. "github.com/gofiber/fiber/v2/middleware/filesystem"
  6. "net/http"
  7. )
  8. //go:embed templates
  9. var tmpl embed.FS
  10. //go:embed static
  11. var static embed.FS
  12. func main() {
  13. app := fiber.New()
  14. app.Use("/", filesystem.New(filesystem.Config{
  15. Root: http.FS(static),
  16. }))
  17. app.Listen(":3000")
  18. }

同样的也是使用http.FS函数,然后结合fiber提供的filesystem达到静态托管文件服务的目的。
运行这段程序,然后在浏览器中输入http://127.0.0.1:3000/static/index.html即可看到效果。
搞定了静态文件托管,那么Fiber HTML模板也就比较容易了,代码如下所示:

  1. package main
  2. import (
  3. "embed"
  4. "github.com/gofiber/fiber/v2"
  5. "github.com/gofiber/template/html"
  6. "net/http"
  7. )
  8. //go:embed templates
  9. var tmpl embed.FS
  10. func main() {
  11. engine := html.NewFileSystem(http.FS(tmpl), ".tmpl")
  12. app := fiber.New(fiber.Config{Views: engine})
  13. app.Get("/", func(ctx *fiber.Ctx) error {
  14. return ctx.Render("templates/index", fiber.Map{"title": "Golang Embed 测试"})
  15. })
  16. app.Listen(":3000")
  17. }

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

小结

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

原文地址:
https://www.flysnow.org/2021/02/28/golang-embed-for-web.html