// Personal website of Chris Smith

Go » Listing Files in a directory

Go 1.16 and above:

files, err := os.ReadDir("./")
if err != nil {
    log.Fatal(err)
}

for _, f := range files {
    fmt.Println(f.Name())
}

Go 1.15 and below:

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

for _, f := range files {
    fmt.Println(f.Name())
}