Introduction and Implementation of Golang Embed
Introduction to Embed Packages
Since Golang released version 1.16 there is a new feature called Embed. This embed package is a feature that makes it easier to open the contents of a file at compile time, automatically inserting the contents of the file into the variables that we have defined. For more details, you can see here .
How to Embed Files
When we want to embed a file into a variable, we can import the embed package first, then we can add the comment //go:embed followed by the file above the variable we are aiming for. The target variable will automatically contain the contents of the file we want when the Golang code is compiled. The target variable cannot be stored in a function.
Embed File to String
Entering the contents of a file into a variable (Embed) will automatically read the contents of the file as text and insert it into the variable.
Before we carry out the implementation, we try to create a folder first with the name learn-golang-embed then also initialize go mod with the command below
1go mod init github.com/santekno/learn-golang-embedAfter that we create a txt file called version.txt which we will later read by the embed program which we will try this time with the version contents, for example like this
1v1.0.0After that, we try to create a main.go file and fill the main file with the program code below.
1package main
2
3import (
4 _ "embed"
5 "fmt"
6)
7
8//go:embed version.txt
9var version string
10
11func main() {
12 fmt.Println(version)
13}So, when we run it it will produce this.
1➜ learn-golang-embed git:(main) ✗ go run main.go
2v1.0.0Embed Files into Bytes
Now we try to embed it into another type, namely []byte. Apart from the string data type, this embed can also be used in several types. This is very suitable if we want to embed files in binary form, for example images and others.
1package main
2
3import (
4 _ "embed"
5 "io/fs"
6 "os"
7)
8
9//go:embed logo.png
10var logo []byte
11
12func main() {
13 err := os.WriteFile("logo_new.png", logo, fs.ModePerm)
14 if err != nil {
15 panic(err)
16 }
17}logo_new.png with the same image as logo.pngEmbed Multiple Files
Usually we also want to embed several files at once for our needs. We can do this by using the embed package by adding //go:embed comments on more than one line and we can use the variable data type embed.FS.
We create a file folder with 3 files with the names a.txt, b.txt and c.txt with the contents of each file AAA, BBB, CCC or just adjust it to your friend’s wishes -Friend.
1package main
2
3import (
4 "embed"
5 _ "embed"
6 "fmt"
7)
8
9//go:embed files/a.txt
10//go:embed files/b.txt
11//go:embed files/c.txt
12var files embed.FS
13
14func main() {
15 a, _ := files.ReadFile("files/a.txt")
16 fmt.Println(string(a))
17
18 b, _ := files.ReadFile("files/b.txt")
19 fmt.Println(string(b))
20
21 c, _ := files.ReadFile("files/c.txt")
22 fmt.Println(string(c))
23}Path Matcher
Apart from manually reading one file at a time, we can also use a patch matcher to read multiple files that we want. We can implement this when we have a need to open files by having a pattern of the types of files we read. How to? We need to use path matcher in the path.Match function package
1package main
2
3import (
4 "embed"
5 _ "embed"
6 "fmt"
7)
8
9//go:embed files/*.txt
10var path embed.FS
11
12func main() {
13 dir, _ := path.ReadDir("files")
14 for _, entry := range dir {
15 if !entry.IsDir() {
16 fmt.Println(entry.Name())
17 content, _ := path.ReadFile("files/" + entry.Name())
18 fmt.Println("Content: ", string(content))
19 }
20 }
21}1➜ learn-golang-embed git:(main) ✗ go run main.go
2a.txt
3Content: AAA
4b.txt
5Content: BBB
6c.txt
7Content: CCCCompile Embed Results
We need to know that the results of the embed carried out by the embed package are permanent and the file data that is read will be saved into the Golang binary file that we have created. So this embed package does not read external files in real time so that if we have compiled it and have a Golang binary file, then we no longer need the external file and if we change the external file then the contents of the variables will not change again, so we need to re-compile.