Home >Backend Development >Golang >What is embed? How does Go use it to load static files?
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:
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
docker
and dockerfile
, which is very troublesome. 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
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)}
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)}
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))}
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.
// 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)
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))}
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))}
/, 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!