Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6140337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:06:19+00:00 2026-05-23T18:06:19+00:00

I need to write a function which when given the path of a folder

  • 0
  1. I need to write a function which when given the path of a folder
    scans the files rooted at that folder.
  2. 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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:06:19+00:00Added an answer on May 23, 2026 at 6:06 pm

    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:

    package main
    
    import (
        "flag"
        "fmt"
        "io/fs"
        "path/filepath"
    )
    
    func visit(path string, di fs.DirEntry, err error) error {
        fmt.Printf("Visited: %s\n", path)
        return nil
    }
    
    func main() {
        flag.Parse()
        root := flag.Arg(0)
        err := filepath.WalkDir(root, visit)
        fmt.Printf("filepath.WalkDir() returned %v\n", err)
    }
    

    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.

    package main
    
    import (
      "path/filepath"
      "os"
      "flag"
      "fmt"
    )
    
    func visit(path string, f os.FileInfo, err error) error {
      fmt.Printf("Visited: %s\n", path)
      return nil
    } 
    
    
    func main() {
      flag.Parse()
      root := flag.Arg(0)
      err := filepath.Walk(root, visit)
      fmt.Printf("filepath.Walk() returned %v\n", err)
    }
    

    Please note that filepath.Walk walks the directory tree recursively.

    This is an example run:

    $ mkdir -p dir1/dir2
    $ touch dir1/file1 dir1/dir2/file2
    $ go run walk.go dir1
    Visited: dir1
    Visited: dir1/dir2
    Visited: dir1/dir2/file2
    Visited: dir1/file1
    filepath.Walk() returned <nil>
    

    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.

    package main
    
    import (
        "path/filepath"
        "os"
        "flag"
    )
    
    type visitor int
    
    // THIS CODE NO LONGER WORKS, PLEASE SEE ABOVE
    func (v visitor) VisitDir(path string, f *os.FileInfo) bool {
        println(path)
        return true
    } 
    
    func (v visitor) VisitFile(path string, f *os.FileInfo) {
        println(path)
    }
    
    func main() {
        root := flag.Arg(0)
        filepath.Walk(root, visitor(0), nil)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Searching through list I need to write a function 'once' which, given
I need to write a function that splits records into separate files based on
I need to write a function that takes 4 bytes as input, performs a
I need to write a function that is passed an instance of a CharField.
I need to write a delegate function that can 'wrap' some while/try/catch code around
I need to write a little ruby function that does word wrapping. I have
I need to be able to write a function that shows repeated words from
Basically what I need to do is write a function that takes in a
I need to write, in JavaScript (I am using jQuery), a function that is
I need write an update statement that used multiple tables to determine which rows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.