<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Manigandan Dharmalingam on Medium]]></title>
        <description><![CDATA[Stories by Manigandan Dharmalingam on Medium]]></description>
        <link>https://medium.com/@manigandand?source=rss-339829e6ed34------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*83YF9NfkxisUAM_dKB4maw.jpeg</url>
            <title>Stories by Manigandan Dharmalingam on Medium</title>
            <link>https://medium.com/@manigandand?source=rss-339829e6ed34------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 10:26:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@manigandand/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[List files in a directory using golang]]></title>
            <link>https://medium.com/@manigandand/list-files-in-a-directory-using-golang-963b1df11304?source=rss-339829e6ed34------2</link>
            <guid isPermaLink="false">https://medium.com/p/963b1df11304</guid>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Manigandan Dharmalingam]]></dc:creator>
            <pubDate>Thu, 08 Mar 2018 17:31:26 GMT</pubDate>
            <atom:updated>2018-03-08T17:42:58.964Z</atom:updated>
            <content:encoded><![CDATA[<p>We can get a list of files inside a folder on the file system using various golang standard library functions.</p><ul><li><strong>filepath.Walk</strong></li><li><strong>ioutil.ReadDir</strong></li><li><strong>os.File.Readdir</strong></li></ul><p><a href="https://gist.github.com/manigandand/19aa61c81aaabc0962157380e575c006#file-list_directory-go">https://gist.github.com/manigandand/19aa61c81aaabc0962157380e575c006#file-list_directory-go</a></p><h4><strong>Using filepath.Walk</strong></h4><blockquote>The <em>path/filepath package provides handy way to scan all the files in a directory, it will automatically scan each sub-directories in the directory.</em></blockquote><p><a href="https://golang.org/pkg/path/filepath/#Walk"><strong>Walk</strong></a> walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root. All errors that arise visiting files and directories are filtered by walkFn. The files are walked in lexical order, which makes the output deterministic but means that for very large directories Walk can be inefficient. Walk does not follow symbolic links.</p><pre>package main</pre><pre>import (<br> &quot;io/ioutil&quot;<br> &quot;log&quot;<br> &quot;os&quot;<br> &quot;path/filepath&quot;<br>)</pre><pre>func main() {<br> var root string</pre><pre>if len(os.Args) == 1 {<br>  log.Fatal(&quot;No path given, Please specify path.&quot;)<br>  return<br> }<br> if root = os.Args[1]; root == &quot;&quot; {<br>  log.Fatal(&quot;No path given, Please specify path.&quot;)<br>  return<br> }<br> // filepath.Walk<br> files, err := FilePathWalkDir(root)<br> if err != nil {<br>  panic(err)<br> }</pre><pre> for _, file := range files{<br>  fmt.Println(file)<br> }<br>}</pre><pre>func FilePathWalkDir(root string) ([]string, error) {<br> var files []string<br> err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {<br>  if !info.IsDir() {<br>   files = append(files, path)<br>  }<br>  return nil<br> })<br> return files, err<br>}</pre><h4>Using ioutil.ReadDir</h4><blockquote><a href="https://golang.org/pkg/io/ioutil/#ReadDir">ioutil.ReadDir</a><em> reads the directory named by dirname and returns a list of directory entries sorted by filename.</em></blockquote><pre>func IOReadDir(root string) ([]string, error) {<br> var files []string<br> fileInfo, err := ioutil.ReadDir(root)<br> if err != nil {<br>  return files, err<br> }</pre><pre>for _, file := range fileInfo {<br>  files = append(files, file.Name())<br> }<br> return files, nil<br>}</pre><h4>Using os.File.Readdir</h4><pre>func (f *<a href="https://golang.org/pkg/os/#File">File</a>) Readdir(n <a href="https://golang.org/pkg/builtin/#int">int</a>) ([]<a href="https://golang.org/pkg/os/#FileInfo">FileInfo</a>, <a href="https://golang.org/pkg/builtin/#error">error</a>)</pre><blockquote><a href="https://golang.org/pkg/os/#File.Readdir"><strong>Readdir</strong></a> reads the contents of the directory associated with file and returns a slice of up to n FileInfo values, as would be returned by Lstat, in directory order. Subsequent calls on the same file will yield further FileInfos.</blockquote><pre>func OSReadDir(root string) ([]string, error) {<br> var files []string<br> f, err := os.Open(root)<br> if err != nil {<br>  return files, err<br> }<br> fileInfo, err := f.Readdir(-1)<br> f.Close()<br> if err != nil {<br>  return files, err<br> }</pre><pre>for _, file := range fileInfo {<br>  files = append(files, file.Name())<br> }<br> return files, nil<br>}</pre><p>See the benchmark result of these three functions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*gzFp0uHx_DmG-b97j1t8UQ.png" /><figcaption>benchmark results</figcaption></figure><pre>package main</pre><pre>import (<br> &quot;testing&quot;<br>)</pre><pre>var root string = &quot;/home/manigandan/Desktop/Manigandan/sample&quot;</pre><pre>func BenchmarkFilePathWalkDir1(b *testing.B)  { benchmarkFilePathWalkDir(1, root, b) }<br>func BenchmarkFilePathWalkDir2(b *testing.B)  { benchmarkFilePathWalkDir(2, root, b) }<br>func BenchmarkFilePathWalkDir3(b *testing.B)  { benchmarkFilePathWalkDir(3, root, b) }<br>func BenchmarkFilePathWalkDir10(b *testing.B) { benchmarkFilePathWalkDir(10, root, b) }<br>func BenchmarkFilePathWalkDir20(b *testing.B) { benchmarkFilePathWalkDir(20, root, b) }<br>func BenchmarkFilePathWalkDir30(b *testing.B) { benchmarkFilePathWalkDir(30, root, b) }<br>func BenchmarkFilePathWalkDir40(b *testing.B) { benchmarkFilePathWalkDir(40, root, b) }<br>func BenchmarkFilePathWalkDir50(b *testing.B) { benchmarkFilePathWalkDir(50, root, b) }</pre><pre>func BenchmarkIOReadDir1(b *testing.B)  { benchmarkIOReadDir(1, root, b) }<br>func BenchmarkIOReadDir2(b *testing.B)  { benchmarkIOReadDir(2, root, b) }<br>func BenchmarkIOReadDir3(b *testing.B)  { benchmarkIOReadDir(3, root, b) }<br>func BenchmarkIOReadDir10(b *testing.B) { benchmarkIOReadDir(10, root, b) }<br>func BenchmarkIOReadDir20(b *testing.B) { benchmarkIOReadDir(20, root, b) }<br>func BenchmarkIOReadDir30(b *testing.B) { benchmarkIOReadDir(30, root, b) }<br>func BenchmarkIOReadDir40(b *testing.B) { benchmarkIOReadDir(40, root, b) }<br>func BenchmarkIOReadDir50(b *testing.B) { benchmarkIOReadDir(50, root, b) }</pre><pre>func BenchmarkOSReadDir1(b *testing.B)  { benchmarkOSReadDir(1, root, b) }<br>func BenchmarkOSReadDir2(b *testing.B)  { benchmarkOSReadDir(2, root, b) }<br>func BenchmarkOSReadDir3(b *testing.B)  { benchmarkOSReadDir(3, root, b) }<br>func BenchmarkOSReadDir10(b *testing.B) { benchmarkOSReadDir(10, root, b) }<br>func BenchmarkOSReadDir20(b *testing.B) { benchmarkOSReadDir(20, root, b) }<br>func BenchmarkOSReadDir30(b *testing.B) { benchmarkOSReadDir(30, root, b) }<br>func BenchmarkOSReadDir40(b *testing.B) { benchmarkOSReadDir(40, root, b) }<br>func BenchmarkOSReadDir50(b *testing.B) { benchmarkOSReadDir(50, root, b) }</pre><pre>func benchmarkFilePathWalkDir(i int, root string, b *testing.B) {<br> // run the FilePathWalkDir function b.N times<br> for n := 0; n &lt; b.N; n++ {<br>  FpW(i, root)<br> }<br>}</pre><pre>func benchmarkIOReadDir(i int, root string, b *testing.B) {<br> // run the IOReadDir function b.N times<br> for n := 0; n &lt; b.N; n++ {<br>  IoRdir(i, root)<br> }<br>}</pre><pre>func benchmarkOSReadDir(i int, root string, b *testing.B) {<br> // run the OSReadDir function b.N times<br> for n := 0; n &lt; b.N; n++ {<br>  OsRdir(i, root)<br> }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=963b1df11304" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>