How to get a list of files and folders in a directory in Go? (ioutil.ReadDir vs os.ReadDir)
Hello everyone! I just started learning Go and encountered a task: I need to read the contents of a folder and output a list of all files and subfolders. I googled it and found several ways, but I can't figure out which one is correct and modern.\n\nFor example, I tried using `ioutil.ReadDir`, but the documentation says this package is deprecated. I also saw `os.ReadDir` and `filepath.Walk`. I have this code:\n\n```go\npackage main\n\nimport (\n "fmt"\n "io/ioutil"\n "log"\n)\n\nfunc main() {\n files, err := ioutil.ReadDir(".")\n if err != nil {\n log.Fatal(err)\n }\n for _, f := range files {\n fmt.Println(f.Name())\n }\n}\n```\n\nIt works, but the IDE underlines `ioutil` and says it's deprecated. How to correctly get a list of files in Go 1.1
No answers yet.
Log in to leave an answer
Blogs
Optimizing JavaScript Application Performance on Node.js
Generics in TypeScript: Why They Are Needed and How to Use Them in Practice
Microservices in C#: Architecture and Practice from A to Z
Go in 2025: What's New? A Complete Overview for Beginners