golang document it is easier to find a method is filepath. Walk, this method has a problem of the current directory is automatically recursive traversal subdirectory, in fact, we usually just want to get a directory file list below, does not need so much information, at the same time this method is much more complicated to write code, we have no need to do so.
if you simply want to get a list of files and folders under a directory, there are two easy ways to do
USES ioutil’s ReadDir method
package main
import (
"fmt"
"io/ioutil"
)
func main() {
files, _ := ioutil.ReadDir("./")
for _, f := range files {
fmt.Println(f.Name())
}
}
using filepath Glob method
package main
import (
"fmt"
"path/filepath"
)
func main() {
files, _ := filepath.Glob("*")
fmt.Println(files) // contains a list of all files in the current directory
}
Read More:
- The too many open files solution appears in stream classes such as files.list
- Delete files with specified suffix in specified folder under Linux
- In J2EE Tomcat webapps, there are only folder directories and no files
- Ubuntu reads the shared folder under windows Unable to mount location Failed to retrieve share list from server
- The out folder does not appear under the idea project. Set the entire compilation information to the target folder
- Golang determines whether the directory is empty
- Error: Projects must list all files or use an ‘include‘ pattern.
- How to batch create folders in Excel form and copy the specified files to a new folder
- How to get all the keys of map by golang
- macbook golang zsh: exec format error
- Ida batch processes virus share samples to obtain ASM files and bytes files
- How to compare the equality of structures in golang
- Golang timer function executes a function every few minutes
- Method of modifying file and folder permission by Chmod command in Linux
- The node rimraf module recursively deletes the contents of the folder
- [Solved] Golang Error: The system cannot find the path specified. [mkdir C:/xx/yy/]:
- Vscode compiles multiple files, compiles files in subfolders
- Prompt 550 remove directory operation failed when FTP delete folder
- Perl application – delete “. SVN” folder in current directory and subdirectory
- Linux view folder size, remaining disk space (DU / DF)