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 replace ioutil.ReadDir(), which returns []FileInfo. Although neither os.ReadDir() nor os.DirEntry offer any new functionality, they make things faster and simpler, which is important.

  • The os.ReadFile() function directly replaces ioutil.ReadFile().

  • The os.WriteFile() function can directly replace ioutil.WriteFile().

  • Similarly, os.MkdirTemp() can replace ioutil.TempDir() without any changes. However, because the os.TempDir() name was already taken, the new function name is different.

  • The os.CreateTemp() function is the same as ioutil.TempFile(). Although the name os.TempFile() was not taken, the Go people decided to name it os.CreateTemp() in order to be on par with os.MkdirTemp().

Get hands-on with 1400+ tech skills courses.