

This is great when all you need to do is operate on files one by one, but as Go programmers we're often concerned with collections of files. What even is a file?Īs Go programmers we're used to reading and writing data to disk files using the abstract byte-stream interfaces io.Reader and io.Writer, and we're also familiar with operations on individual files using the standard os package, particularly the *os.File type, which represents an individual file or directory. Walk with me, then, as we take a tour of the new io/fs package, the fs.FS interface in particular, and the power of the filesystem abstraction.
#.fs file file secure code
That means we can also write super-fast and efficient tests on filesystem code using in-memory structures. The standard library's io/fs package provides great facilities for working with these data structures, especially when we need to recursively walk filesystem trees, selectively performing operations on their nodes.Īnd it's easy to implement fs.FS we can do it with a Go map, for example. A tree of disk files is the obvious example, but if we design our program to operate on an fs.FS value, it can also process ZIP and tar archives, Go modules, arbitrary JSON, YAML, or CUE data, or even Web resources addressed by URLs. In principle, any set of objects that can be addressed by a hierarchy of pathnames can be represented by an fs.FS.

In fact, the fs.FS interface can be used with more than just files: it abstracts the idea of a path-value map. The new io/fs package introduced in Go 1.16 gives us a powerful new way of working with filesystems: that is, trees of files.

To understand recursion, you must first understand recursion.
