#ioutil.ReadDir() is deperecated, what's the new way to check if file is a directory or file

4 messages Β· Page 1 of 1 (latest)

left blade
#

hello everyone noob here πŸ™‚
i'm following this book with an example on how to list the files in a directory and determine if the it is a directory or not.

func main() {
    files, err := ioutil.ReadDir("my_directory")
    if err != nil {
        log.Fatal(err)
    }

    for _, file := range files {
        if !file.IsDir() {
            fmt.Println("file:", file.Name())
            continue
        }
        fmt.Println("directory:", file.Name())
    }
}

but my ide is complaining that ioutil.ReadDir() is deprecated. what is the updated way to do this task? πŸ™‚

#

from the docs(https://pkg.go.dev/io/ioutil) it says
| Deprecated: As of Go 1.16, this value is simply io.Discard.
but i don't understand how to use io.Discard

tacit monolith
#

Deprecated: As of Go 1.16, os.ReadDir is a more efficient and correct choice: it returns a list of fs.DirEntry instead of fs.FileInfo, and it returns partial results in the case of an error midway through reading a directory.