SoFunction
Updated on 2025-03-04

Two ways to package static files in Go

1. Preface

When developing applications using Go, you sometimes encounter situations where you need to read static resources.

  • For example: when developing a web application, the program needs to load the template file to generate output HTML. When deploying a program, in addition to publishing the application executable file, it also needs to publish the dependent static resource file. This adds some trouble to the release process.

  • For example: There are some static templates in the project that need to be rendered, but these templates are fixed static files.

If you don't package this kind of static file: it is relatively troublesome to publish and mount this kind of static file separately, someone will find a way to package the static resource file into Go's program file.

The following are two packaging methods: go-bindata, go:embed

2. Go-bindata packages static resources

go-bindata encapsulates any file in a Go language Source Code. When converting file data to original bytes, you can choose to use gzip compression. It also provides a unified interface to help obtain original file data.

2.1 Installation

go get -u /go-bindata/go-bindata/...

Install the packaging tool go-bindata toGOPATH/bin, enter the following command to check whether the installation is successful:

go-bindata  --version

2.2 Packaging

go-bindata -o= -pkg=resource   ./resource/...
  • Will put./resourceAll files in the directory are converted into Go code, pay attention to the following... This means that all files and sub-files below are packaged.
  • Generate a name calledThe file you can use in your Go codemainpackage to access these static files.

If you view the source file, you can view it_bindataMaintained file information

2.3 Use

  • Asset(name string) ([]byte, error): Return file content based on resource name
  • MustAsset(name string) []byte: Return file content based on resource name
  • AssetInfo(name string) (, error): Return file information based on resource name
  • AssetNames() []string: Returns all resource names
  • AssetDir(name string) ([]string, error) : Returns the name of a certain folder layer
  • RestoreAsset(dir, name string) error
  • RestoreAssets(dir, name string) error

3. go:embed package static resources

Starting from Go version 1.16, you can use go:embed to embed files.

embed is a newly added package in Go 1.16. It passes//go:embedDirectives can package static resource files into compiled programs (exe) during the compilation stage and provide the ability to access these files.

  • For individual files, the embed type is supportedstringandbyte slice
  • Supports embedding as new file system FS for multiple files and folders
  • For example, import"embed"Pack, evenNo explicit use
  • go:embed directive is used for embedding, and must be followed by the embedded variable name.

Note: The use of go embed can only be used for variables declared at the package level (Global variables)

3.1 embed the file to be embedded

//go:embed file to be embedded
  • Example 1
import (
    _ "embed"
    "fmt"
)
​
//go:embed 
var data string
​
func main() {
    (data)
}
  • Example 2
import (
    _ "embed"
    "fmt"
)
​
//go:embed 
var data []byte
​
func main() {
    (string(data))
}

3.2 Embed file system FS

Supports embedding as new file system FS for multiple files and folders

Three methods are provided for access:

Open(name string) (, error)   According to resourceskeyReturn to file
​
ReadFile(name string) ([]byte, error)  According to resourceskeyReturn to file内容
​
ReadDir(name string) ([], error)  according tokeyRead folder   
  • Embed folder

useEmbed file variables have 3 functions, as shown below:

import (
    "embed"
    _ "embed"
    "fmt"
)
​
//go:embed resource
var data 
​
func main() {
    // Read the file directly    file, err := ("resource/cmd/")
    (string(file), err)
}
  • Embed multiple files
import (
    "embed"
    _ "embed"
    "fmt"
)
​
//go:embed  
var data 
​
func main() {
    file, err := ("")
    (string(file), err)
}

This is the end of this article about two ways to package static files in Go. For more related contents of Go static files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!