How can I list all files in Go language?
In Go language, you can use the ReadDir function in the ioutil package to list all files and folders in a specific directory. Here is a simple example code:
package main
import (
"fmt"
"io/ioutil"
)
func main() {
dir := "./" // 指定目录
files, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Println("读取目录失败:", err)
return
}
for _, file := range files {
fmt.Println(file.Name())
}
}
In the example above, we specified the directory to list files from as the current directory (./). The ioutil.ReadDir function will return a slice of type []os.FileInfo, which contains information about all the files and folders in the directory. By iterating through this slice, we can access the name of each file.