ReadDir and DirEntry
Let’s learn about the functionality of ReadDir and DirEntry.
We'll cover the following
This lesson discusses os.ReadDir()
and os.DirEntry
. However, it begins by discussing the deprecation of the io/ioutil
package—the functionality of the io/ioutil
package has been transferred to other packages. So, we have the following:
os.ReadDir()
, which is a new function, returns[]DirEntry
. This means that it can’t directly replaceioutil.ReadDir()
, which returns[]FileInfo
. Although neitheros.ReadDir()
noros.DirEntry
offer any new functionality, they make things faster and simpler, which is important.The
os.ReadFile()
function directly replacesioutil.ReadFile()
.The
os.WriteFile()
function can directly replaceioutil.WriteFile()
.Similarly,
os.MkdirTemp()
can replaceioutil.TempDir()
without any changes. However, because theos.TempDir()
name was already taken, the new function name is different.The
os.CreateTemp()
function is the same asioutil.TempFile()
. Although the nameos.TempFile()
was not taken, the Go people decided to name itos.CreateTemp()
in order to be on par withos.MkdirTemp()
.
Get hands-on with 1400+ tech skills courses.