Embedding Files

Let’s learn about some more features.

We'll cover the following

Now, let’s discuss some systems programming features including embedding files in Go binaries as well as the introduction of the os.ReadDir() function, the os.DirEntry type, and the io/fs package. We begin by presenting the embedding of files into Go binary executables.

This feature first appeared in Go 1.16 and allows us to embed static assets into Go binaries. The allowed data types for keeping an embedded file are string, []byte, and embed.FS. This means that a Go binary may contain a file that we do not have to manually download when we execute the Go binary! The presented utility embeds two different files that it can retrieve based on the given command-line argument.

Coding example

The code that follows, which is saved as embedFiles.go, illustrates this new Go feature:

Get hands-on with 1200+ tech skills courses.