New to Go 1.16
Go 1.16 came with some new 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.
As these features are related to systems programming, they are included and explored in the current chapter. We begin by presenting the embedding of files into Go binary executables.
Embedding files
This section presents a feature that first appeared in Go 1.16 that allows you 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 you do not have to manually download when you execute the Go binary! The presented utility embeds two different files that it can retrieve based on the given command-line argument.
The code that follows, which is saved as embedFiles.go
, illustrates this new Go feature:
package main
import (
_ "embed"
...