1. Write it in front
Chinese name | English name | illustrate |
---|---|---|
Table of contents | directory |
A collection of files and folders |
Folders | folder |
Container for storing files and folders |
document | file |
A single unit of data used to store information (text, images, sound, etc.) |
path | path |
The path used to locate files |
We use the command linetree /?
andtree /F
Check the tree directory as follows:
D:\Go_WorkSpace\p3-traversal>tree /?
Graphically displays the folder structure of the drive or path.TREE [drive:][path] [/F] [/A]
/F Displays the name of the file in each folder.
/A Use ASCII characters instead of extended characters.
D:\Go_WorkSpace\p3-traversal>tree /F
Folder PATH List
Volume serial number is 277B-1704
D:.
│
│ main_filepath_glob.go
│ main_filepath_walk.go
│ main_ioutil_ReadDir.go
│
└─Family Directory
│
│
└─Subdirectory
│
│
│
│
└─Sun Directory
2. Use
- This method reads the contents of the directory and returns the contents of each file or directory in the directory
Object slice;
- Can be used
for
Loop through the slice and print out the name of each file or directory;
Examples are as follows:
package main import ( "fmt" "io/ioutil" "log" ) func main() { files, err := (".") if err != nil { (err) } for _, file := range files { (()) } }
Results output:
D:\Go_WorkSpace\p3-traversal>go run main_ioutil_ReadDir.go
main_filepath_glob.go
main_filepath_walk.go
main_ioutil_ReadDir.go
Parent directory
Analysis instructions:
1. Only print the current directory, not the descendants directory;
2. Will not print out.
and..
etc. Special directories;
III. Use
- This method traverses the file tree in the root of the specified directory (
a file tree
) and call the function for each file or directory encountered; - Anonymous functions can be used (
an anonymous function
) Print out the name of each file or directory
Examples are as follows:
package main import ( "fmt" "os" "path/filepath" ) func main() { err := (".", func(path string, info , err error) error { (path) return nil }) if err != nil { (err) } }
Results output:
D:\Go_WorkSpace\p3-traversal>go run main_filepath_walk.go
.
main_filepath_glob.go
main_filepath_walk.go
main_ioutil_ReadDir.go
Parent directory
Parent directory\
Parent directory\Subdirectory
Parent directory\Subdirectory\
Parent directory\Subdirectory\
Parent directory\Subdirectory\
Parent directory\Subdirectory\Summary directory
Parent directory\Subdirectory\Summary directory\Summary directory\
Parent directory\Subdirectory\Summary directory\Summary directory\
Analysis instructions:
1. Print the current directory, including the descendants directory;
2. Include.
Special directories such as traversal need to be excluded;
4. Use
- This method returns a list of file names that match the specified pattern;
- Wildcard characters can be used (
*
) to match all files in the current directory
Examples are as follows:
package main import ( "fmt" "log" "os" "path/filepath" ) func main() { pwd, _ := () filePaths, err := ((pwd, "*")) if err != nil { (err) } for _, filePath := range filePaths { (filePath) } }
Results output:
D:\Go_WorkSpace\p3-traversal>go run main_filepath_glob.go
D:\Go_WorkSpace\p3-traversal\
D:\Go_WorkSpace\p3-traversal\main_filepath_glob.go
D:\Go_WorkSpace\p3-traversal\main_filepath_walk.go
D:\Go_WorkSpace\p3-traversal\main_ioutil_ReadDir.go
D:\Go_WorkSpace\p3-traversal\parent directory
Analysis instructions:
1. Only print the current directory, not the descendants directory;
2. Will not print out.
and..
etc. Special directories;
3. The return isAbsolute path;
Summarize
This is the end of this article about the three methods of Go language traversing the directory. For more information about Go traversing the directory, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!