Home  >  Article  >  Backend Development  >  What is embed? How does Go use it to load static files?

What is embed? How does Go use it to load static files?

藏色散人
藏色散人forward
2021-11-12 14:31:443798browse

This article is provided by the go language tutorial column to introduce how to use embed to load static files in Golang1.16. I hope it will be helpful to friends in need!

#What is embed

embed is a new package added in Go 1.16. It uses the //go:embed directive to package static resource files into compiled programs during the compilation phase and provide the ability to access these files.

Why embed is needed

In the past, many friends who switched to Go language from other languages ​​would ask, or step into a pit : It is assumed that the binary file packaged by the Go language will include the joint compilation and packaging of the configuration file.

As a result, once the binary file is moved around, the application cannot be run because the resources of the static file cannot be read.

If static resources cannot be compiled and packaged into binary files, there are usually two solutions:

  • The first is to identify such static resources and whether you need to follow the program.
  • The second is to package it into a binary file.

In the second case, Go did not support it before, so everyone will use various fancy open source libraries, such as: go-bindata/go-bindata accomplish.

But starting from Go1.16, the Go language itself officially supports this feature.

It has the following advantages

  • It can package static resources into binary packages, and the deployment process is simpler. Traditional deployment either requires packaging and uploading static resources together with compiled programs, or automating the former using docker and dockerfile, which is very troublesome.
  • Ensure program integrity. Damage or loss of static resources during operation usually affects the normal operation of the program.
  • There is no io operation for static resource access, and the speed will be very fast.

Basic usage of embed

Through the official documentation, we know the three ways of embed: string, bytes and FS (File Systems). Among them, the string and []byte types can only match one file. If you want to match multiple files or a directory, you must use the embed.FS type.

Special note: the embed package must be imported. If the import is not used, just use _ to import

1. Embed as characters String


For example, there is a file hello.txt under the current file, and the content of the file is hello,world!. Through the go:embed instruction, after compilation, the value of the s variable in the following program becomes hello,world!.

package mainimport (
    _ "embed"
    "fmt")//go:embed hello.txtvar s stringfunc main() {
    fmt.Println(s)}

2. Embedding as byte slice


You can also embed the contents of a single file as a slice of byte, which is a byte array.

package mainimport (
    _ "embed"
    "fmt")//go:embed hello.txtvar b []bytefunc main() {
    fmt.Println(b)}

3. Embed as fs.FS


You can even embed as a file system, which is very useful when embedding multiple files.

For example, embed a file:

package mainimport (
    "embed"
    "fmt")//go:embed hello.txtvar f embed.FSfunc main() {
    data, _ := f.ReadFile("hello.txt")
    fmt.Println(string(data))}

Embed another local file hello2.txt, support multiple go:embed instructions on the same variable (embed as string or A byte slice cannot have multiple go:embed instructions):

package mainimport (
    "embed"
    "fmt")//go:embed hello.txt//go:embed hello2.txtvar f embed.FSfunc main() {
    data, _ := f.ReadFile("hello.txt")
    fmt.Println(string(data))
    data, _ = f.ReadFile("hello2.txt")
    fmt.Println(string(data))}

Embedding of the current repeated go:embed instructions into embed.FS is supported, quite Yu one:

package mainimport (
    "embed"
    "fmt")//go:embed hello.txt//go:embed hello.txtvar f embed.FSfunc main() {
    data, _ := f.ReadFile("hello.txt")
    fmt.Println(string(data))}

You can also embed files in subfolders:

package mainimport (
    "embed"
    "fmt")//go:embed p/hello.txt//go:embed p/hello2.txtvar f embed.FSfunc main() {
    data, _ := f.ReadFile("p/hello.txt")
    fmt.Println(string(data))
    data, _ = f.ReadFile("p/hello2.txt")
    fmt.Println(string(data))}

embedAdvanced usage

Go1.16 In order to ## Support for #embed has also been added in a new package io/fs. Combining the two can operate like ordinary files before.

1. Read-only

The embedded content is read-only. That is, what is the content of the embedded file at compile time, then what is the content at runtime.

The FS file system value provides methods for opening and reading, and there is no write method. This means that the FS instance is thread-safe and multiple goroutines can be used concurrently.

embed.FS structure mainly has three external methods, as follows:

// Open 打开要读取的文件,并返回文件的fs.File结构.func (f FS) Open(name string) (fs.File, error)// ReadDir 读取并返回整个命名目录func (f FS) ReadDir(name string) ([]fs.DirEntry, error)// ReadFile 读取并返回name文件的内容.func (f FS) ReadFile(name string) ([]byte, error)

2. Embed multiple files

package mainimport (
    "embed"
    "fmt")//go:embed hello.txt hello2.txtvar f embed.FSfunc main() {
    data, _ := f.ReadFile("hello.txt")
    fmt.Println(string(data))

    data, _ = f.ReadFile("hello2.txt")
    fmt.Println(string(data))}

Of course you You can also write multiple lines like the previous example

go:embed:

package mainimport (
    "embed"
    "fmt")//go:embed hello.txt//go:embed hello2.txtvar f embed.FSfunc main() {
    data, _ := f.ReadFile("hello.txt")
    fmt.Println(string(data))
    data, _ = f.ReadFile("hello2.txt")
    fmt.Println(string(data))}

3. Support folder separation


The characters use forward slashes

/, even Windows systems also use this pattern.

package mainimport (
    "embed"
    "fmt")//go:embed pvar f embed.FSfunc main() {
    data, _ := f.ReadFile("p/hello.txt")
    fmt.Println(string(data))
    data, _ = f.ReadFile("p/hello2.txt")
    fmt.Println(string(data))}

应用

在我们的项目中,是将应用的常用的一些配置写在了.env的一个文件上,所以我们在这里就可以使用go:embed指令。

main.go 文件:

//go:embed ".env" "v1d0/.env"var FS embed.FSfunc main(){
    setting.InitSetting(FS)
    manager.InitManager()
    cron.InitCron()
    routersInit := routers.InitRouter()
    readTimeout := setting.ServerSetting.ReadTimeout
    writeTimeout := setting.ServerSetting.WriteTimeout
    endPoint := fmt.Sprintf(":%d", setting.ServerSetting.HttpPort)
    maxHeaderBytes := 1 << 20
    server := &http.Server{
        Addr:           endPoint,
        Handler:        routersInit,
        ReadTimeout:    readTimeout,
        WriteTimeout:   writeTimeout,
        MaxHeaderBytes: maxHeaderBytes,
    }
    server.ListenAndServe()}

setting.go文件:

func InitSetting(FS embed.FS) {
    // 总配置处理
    var err error
    data, err := FS.ReadFile(".env")
    if err != nil {
        log.Fatalf("Fail to parse '.env': %v", err)
    }
    cfg, err = ini.Load(data)
    if err != nil {
        log.Fatal(err)
    }
    mapTo("server", ServerSetting)
    ServerSetting.ReadTimeout  = ServerSetting.ReadTimeout * time.Second
    ServerSetting.WriteTimeout = ServerSetting.WriteTimeout * time.Second    // 分版本配置引入
    v1d0Setting.InitSetting(FS)}

The above is the detailed content of What is embed? How does Go use it to load static files?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete