{"id":1166,"date":"2020-01-16T04:53:33","date_gmt":"2020-01-16T04:53:33","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=1166"},"modified":"2021-04-22T19:38:01","modified_gmt":"2021-04-22T14:08:01","slug":"iterate-over-all-files-and-folders-go","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/iterate-over-all-files-and-folders-go\/","title":{"rendered":"Iterate over all files and folders for a path in Go (Golang)"},"content":{"rendered":"\n

‘Walk’<\/strong> function of ‘filepath’<\/strong> package can be used to recursively iterate over all files\/folder in a directory tree.<\/p>\n\n\n\n

https:\/\/golang.org\/pkg\/path\/filepath\/<\/code><\/pre>\n\n\n\n

‘Walk’<\/strong> function will walk the entire tree rooted at the root path include all subdirectories.  Below is the signature of the function<\/p>\n\n\n\n

type WalkFunc func(path string, info os.FileInfo, err error) error<\/code><\/pre>\n\n\n\n

The WalkFunc will be called for with the path<\/strong> of the file\/folder and fileInfo<\/strong> or the error<\/strong> if any error occurred while walking that file\/folder.<\/p>\n\n\n\n

Some things to note about Walk function<\/p>\n\n\n\n

  • All errors are filtered. An error might arise while opening\/visiting the file<\/li>
  • The function does not follow symbolic links<\/li>
  • The files are walked in lexical order<\/li><\/ul>\n\n\n\n

    Let’s see an example:<\/p>\n\n\n\n

    package main\n\nimport (\n    \"fmt\"\n    \"log\"\n    \"os\"\n    \"path\/filepath\"\n)\n\nfunc main() {\n    currentDirectory, err := os.Getwd()\n    if err != nil {\n        log.Fatal(err)\n    }\n    iterate(currentDirectory)\n}\n\nfunc iterate(path string) {\n    filepath.Walk(path, func(path string, info os.FileInfo, err error) error {\n        if err != nil {\n            log.Fatalf(err.Error())\n        }\n        fmt.Printf(\"File Name: %s\\n\", info.Name())\n        return nil\n    })\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

    ‘Walk’ function of ‘filepath’ package can be used to recursively iterate over all files\/folder in a directory tree. ‘Walk’ function will walk the entire tree rooted at the root path include all…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"class_list":["post-1166","post","type-post","status-publish","format-standard","hentry","category-tech"],"yoast_head":"\nIterate over all files and folders for a path in Go (Golang) - Welcome To Golang By Example<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/golangbyexamples.com\/iterate-over-all-files-and-folders-go\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Iterate over all files and folders for a path in Go (Golang) - Welcome To Golang By Example\" \/>\n<meta property=\"og:description\" content=\"‘Walk’ function of ‘filepath’ package can be used to recursively iterate over all files\/folder in a directory tree. ‘Walk’ function will walk the entire tree rooted at the root path include all...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/golangbyexamples.com\/iterate-over-all-files-and-folders-go\/\" \/>\n<meta property=\"og:site_name\" content=\"Welcome To Golang By Example\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-16T04:53:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-22T14:08:01+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/golangbyexamples.com\/iterate-over-all-files-and-folders-go\/\",\"url\":\"https:\/\/golangbyexamples.com\/iterate-over-all-files-and-folders-go\/\",\"name\":\"Iterate over all files and folders for a path in Go (Golang) - Welcome To Golang By Example\",\"isPartOf\":{\"@id\":\"https:\/\/golangbyexamples.com\/#website\"},\"datePublished\":\"2020-01-16T04:53:33+00:00\",\"dateModified\":\"2021-04-22T14:08:01+00:00\",\"author\":{\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\"},\"breadcrumb\":{\"@id\":\"https:\/\/golangbyexamples.com\/iterate-over-all-files-and-folders-go\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/golangbyexamples.com\/iterate-over-all-files-and-folders-go\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/golangbyexamples.com\/iterate-over-all-files-and-folders-go\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/golangbyexamples.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Iterate over all files and folders for a path in Go (Golang)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/golangbyexamples.com\/#website\",\"url\":\"https:\/\/golangbyexamples.com\/\",\"name\":\"Welcome To Golang By Example\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/golangbyexamples.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/golangbyexamples.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Iterate over all files and folders for a path in Go (Golang) - Welcome To Golang By Example","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/golangbyexamples.com\/iterate-over-all-files-and-folders-go\/","og_locale":"en_US","og_type":"article","og_title":"Iterate over all files and folders for a path in Go (Golang) - Welcome To Golang By Example","og_description":"‘Walk’ function of ‘filepath’ package can be used to recursively iterate over all files\/folder in a directory tree. ‘Walk’ function will walk the entire tree rooted at the root path include all...","og_url":"https:\/\/golangbyexamples.com\/iterate-over-all-files-and-folders-go\/","og_site_name":"Welcome To Golang By Example","article_published_time":"2020-01-16T04:53:33+00:00","article_modified_time":"2021-04-22T14:08:01+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/golangbyexamples.com\/iterate-over-all-files-and-folders-go\/","url":"https:\/\/golangbyexamples.com\/iterate-over-all-files-and-folders-go\/","name":"Iterate over all files and folders for a path in Go (Golang) - Welcome To Golang By Example","isPartOf":{"@id":"https:\/\/golangbyexamples.com\/#website"},"datePublished":"2020-01-16T04:53:33+00:00","dateModified":"2021-04-22T14:08:01+00:00","author":{"@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f"},"breadcrumb":{"@id":"https:\/\/golangbyexamples.com\/iterate-over-all-files-and-folders-go\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/golangbyexamples.com\/iterate-over-all-files-and-folders-go\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/golangbyexamples.com\/iterate-over-all-files-and-folders-go\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/golangbyexamples.com\/"},{"@type":"ListItem","position":2,"name":"Iterate over all files and folders for a path in Go (Golang)"}]},{"@type":"WebSite","@id":"https:\/\/golangbyexamples.com\/#website","url":"https:\/\/golangbyexamples.com\/","name":"Welcome To Golang By Example","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/golangbyexamples.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/golangbyexamples.com\/author\/admin\/"}]}},"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paOs1b-iO","amp_validity":null,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/1166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/comments?post=1166"}],"version-history":[{"count":4,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/1166\/revisions"}],"predecessor-version":[{"id":5495,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/1166\/revisions\/5495"}],"wp:attachment":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/media?parent=1166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/categories?post=1166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/tags?post=1166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}