[Go] List entries in a directory

Created on November 30, 2026

Use ioutil.ReadDir to list the names of files and subdirectories in a directory. This example lists the immediate entries of /tmp/dummy_files.

.
├── hello.txt
├── sub
│   └── goodbye.txt
└── world.txt
package main

import (
    "fmt"
    "io/ioutil"
)

func main() {
    files, err := ioutil.ReadDir("/tmp/dummy_files")
    if err != nil {
        panic(err)
    }

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

Output

hello.txt
sub
world.txt

For new Go code, os.ReadDir is the recommended replacement for ioutil.ReadDir.

Go