Avatar ·

How to get list of files and folders in a directory in Go: complete guide

Hello everyone! I'm new to Go and trying to solve a simple task: I need to get a list of all files and subfolders in a given directory. I tried using the `ioutil.ReadDir` function, but it's marked as deprecated. Then I came across `os.ReadDir`, but I didn't fully understand how to work with it. Here's what I tried: ```go package main import ( "fmt" "io/ioutil" ) func main() { files, err := ioutil.ReadDir(".") if err != nil { fmt.Println("Error:", err) return } for _, f := range files { fmt.Println(f.Name()) } } ``` The code compiles, but the IDE highlights `ioutil.ReadDir` and says it's deprecated. I want to use a modern approach. Additionally, I need to: 1. Get only files (without folders). 2. Get only p