SoFunction
Updated on 2025-03-05

Detailed explanation of golang official embed files into executable programs

Preface

I looked up the method of embedding files on the Internet before I officially released the method of Go and I also studied it myself. Although there is no problem, since the official support is still available.
Check out the go source codeembed/Very simple, and readembed/internal/embedtest/embed_test.goJust know how to use it.

Sample program

Embed files directly access

The principle is to usego:embedTags to complete. The following are the following points to read the file directly.
File is notutf8When encoding, the output content will be garbled in Chinese.
If you have tested that embedded files can only be files under the same level as the source code file, and if you have tried other directories, you will report an error.
I have tested several scenarios that I can imagine, and some of them will report errors, so you need to pay attention to when using them.

package main
 
import (
  _ "embed"
)
 
//go:embed 
var testString string // The current directory is resolved to string type 
//go:embed 
var testByte []byte // The current directory is resolved to []byte type 
//go:embed test/
var testAbsolutePath string // Subdirectory, parsed to string type 
//go:embed notExistsFile
var testErr0 string // The file does not exist, compile error: pattern notExistsFile: no matching files found 
//go:embed dir
var testErr1 string // dir is a directory, compile error: pattern dir: cannot embed directory dir: contains no embeddable files 
//go:embed ../
var testErr2 string // Relative path, not the current directory or subdirectory, compile error: pattern ../: invalid pattern syntax 
//go:embed D:\
var testErr3 string // Absolute path, compile error: pattern D:\: no matching files found 
func main() {
  println(testString)
  println(string(testByte))
  println(testAbsolutePath)
}

Embed file list

package main
 
import (
  "embed"
  "io"
  "os"
)
 
//go:embed   test1*.txt
//go:embed test/ test/
//go:embed test0
var fileList 
/*
 Use the above method to add multiple files or directories to a fileList.
 1. Add multiple files and supports "*" wild files.
 2. Support subdirectory files.
 3. Support embedding of a directory.
 */
 
func main() {
  testDir, err := ("test0")
  if err != nil {
    panic(err)
  }
  for _, v := range testDir {
    println(()) // Print the embedded directory contents  }
 
  // Use it to generate an object, which can be read out like a file stream  testFile, err := ("")
  if err != nil {
    panic(err)
  }
  (, testFile)
 
  testFile, err = ("")
  if err != nil {
    panic(err)
  }
  (, testFile)
 
  testFile, err = ("test/")
  if err != nil {
    panic(err)
  }
  (, testFile)
 
  // Read the file content directly  data, err := ("")
  if err != nil {
    panic(err)
  }
  println(string(data))
}

Summarize

Today I saw the release of go1.16, and I looked at the features and supported embedding files into executable programs, so I studied it.
I found that by looking at the source code _test test file, you can know how it is used, and you don’t need to search for tutorials everywhere.
Therefore, you must know the truth and why. Otherwise, you cannot use it flexibly if you search other people’s example code every day without knowing the principles.

This is the article about golang's official embed files to executable programs. For more related contents of golang embed files to executable programs, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!