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. p>
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
}
div>
Read More:
- Golang determines whether the directory is empty
- Example of public key signature and verification generated with fabric case
- panic: runtime error: index out of range
- After the go pointer is declared and assigned, a panic: runtime error: invalid memory address or nil pointer reference appears
- [Solved] #command-line-arguments .\main.go:5:4: no new variables on left side of :=
- Golang timer function executes a function every few minutes
- Go run error go run: cannot run non main package
- How to get all the keys of map by golang
- Java – how to shuffle an ArrayList
- Java – read all the files and folders in a certain directory and three methods to get the file name from the file path
- Python error type error: ‘range’ object does not support item assignment, solution
- How to import Python from relative path
- Java 8 Stream – Read a file line by line
- Git – remove and trace files
- Notepad + + has no plug-in manager solution
- Python — using Matplotlib to draw histogram
- Python server run code ModuleNotFoundError Error [How to Solve]
- Yield usage in Python
- TypeError: unsupported operand type(s) for *: ‘range‘ and ‘int‘
- Pre initialization of list content and length in Python