[Go] 指定したディレクトリー内のファイル名とディレクトリー名を列挙する
            作成日: 2022年05月26日
          
          ioutil パッケージの ReadDir 関数を使用すると、指定したディレクトリー内に存在するファイル名とディレクトリー名を列挙することができます。下記の例では、/tmp/dummy_files ディレクトリーの中にあるファイル名とディレクトリー名の一覧を全て出力しています。
まず、下記のようなディレクトリーがあるとします。
.
├── hello.txt
├── sub
│   └── goodgye.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())
    }
}
実行結果は下記のとおりです。
hello.txt
sub
world.txt