Embedding Files in Go Binaries

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab aims to test your understanding of the embed package in Golang. The embed package allows programs to include arbitrary files and folders in the Go binary at build time.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/FileOperationsGroup(["`File Operations`"]) go/FileOperationsGroup -.-> go/embed_directive("`Embed Directive`") subgraph Lab Skills go/embed_directive -.-> lab-15469{{"`Embedding Files in Go Binaries`"}} end

Embed Directive

Your task is to modify the given code to embed files and folders into the Go binary and print their contents.

  • You must use the embed package to embed files and folders.
  • You must use the string and []byte types to store the contents of the embedded files.
  • You must use the embed.FS type to embed multiple files or folders with wildcards.
  • You must print the contents of the embedded files.
## Use these commands to run the example.
## (Note: due to limitation on go playground,
## this example can only be run on your local machine.)
$ mkdir -p folder
$ echo "hello go" > folder/single_file.txt
$ echo "123" > folder/file1.hash
$ echo "456" > folder/file2.hash

$ go run embed-directive.go
hello go
hello go
123
456

There is the full code below:

// `//go:embed` is a [compiler
// directive](https://pkg.go.dev/cmd/compile#hdr-Compiler_Directives) that
// allows programs to include arbitrary files and folders in the Go binary at
// build time. Read more about the embed directive
// [here](https://pkg.go.dev/embed).
package main

// Import the `embed` package; if you don't use any exported
// identifiers from this package, you can do a blank import with `_ "embed"`.
import (
	"embed"
)

// `embed` directives accept paths relative to the directory containing the
// Go source file. This directive embeds the contents of the file into the
// `string` variable immediately following it.
//
//go:embed folder/single_file.txt
var fileString string

// Or embed the contents of the file into a `[]byte`.
//
//go:embed folder/single_file.txt
var fileByte []byte

// We can also embed multiple files or even folders with wildcards. This uses
// a variable of the [embed.FS type](https://pkg.go.dev/embed#FS), which
// implements a simple virtual file system.
//
//go:embed folder/single_file.txt
//go:embed folder/*.hash
var folder embed.FS

func main() {

	// Print out the contents of `single_file.txt`.
	print(fileString)
	print(string(fileByte))

	// Retrieve some files from the embedded folder.
	content1, _ := folder.ReadFile("folder/file1.hash")
	print(string(content1))

	content2, _ := folder.ReadFile("folder/file2.hash")
	print(string(content2))
}

Summary

In this lab, you learned how to use the embed package to embed files and folders into the Go binary at build time. You also learned how to use the string and []byte types to store the contents of the embedded files, and how to use the embed.FS type to embed multiple files or folders with wildcards.

Other Go Tutorials you may like