- I need to write a function which when given the path of a folder
scans the files rooted at that folder. - And then I need to display the directory structure at that folder.
I know how to do 2 (I am going to use jstree to display it in the browser).
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
EDIT FOR 1.16: Enough people still hit this answer, that I thought I’d update it for Go 1.16.
The function filepath.WalkDir introduced in Go 1.16 has better performance than filepath.Walk mentioned in the previous edit. Here’s a working example:
EDIT: Enough people still hit this answer, that I thought I’d update it for the Go1 API. This is a working example of filepath.Walk(). The original is below.
Please note that filepath.Walk walks the directory tree recursively.
This is an example run:
ORIGINAL ANSWER FOLLOWS: The interface for walking file paths has changed as of weekly.2011-09-16, see http://groups.google.com/group/golang-nuts/msg/e304dd9cf196a218. The code below will not work for release versions of GO in the near future.
There’s actually a function in the standard lib just for this: filepath.Walk.