The io/fs Package

Let’s learn about the io/fs package.

Functionality of io/fs package

This lesson illustrates the functionality of the io/fs package, which was first introduced in Go 1.16. Because io/fs offers a unique kind of functionality, we begin this lesson by explaining what io/fs can do. Put simply, io/fs offers a read-only file system interface named FS. Note that embed.FS implements the fs.FS interface, which means that embed.FS can take advantage of some of the functionality offered by the io/fs package. This means that our applications can create their own internal file systems and work with their files.

Coding example

The code example that follows, which is saved as ioFS.go, creates a file system using embed by putting all the files of the ./static folder in there. ioFS.go supports the following functionality: list all files, search for a file name, and extract a file using list(), search(), and extract(), respectively. We begin by presenting the implementation of list():

Get hands-on with 1400+ tech skills courses.