List directory in Go

You can try using the ReadDir function in the io/ioutil package. Per the docs:

ReadDir reads the directory named by dirname and returns a list of sorted directory entries.

The resulting slice contains os.FileInfo types, which provide the methods listed here. Here is a basic example that lists the name of everything in the current directory (folders are included but not specially marked – you can check if an item is a folder by using the IsDir() method):

<code><span class="kwd">package</span><span class="pln"> main

</span><span class="kwd">import</span> <span class="pun">(</span>
    <span class="str">"fmt"</span>
    <span class="str">"io/ioutil"</span>
     <span class="str">"log"</span>
<span class="pun">)</span><span class="pln">

func main</span><span class="pun">()</span> <span class="pun">{</span><span class="pln">
    files</span><span class="pun">,</span><span class="pln"> err </span><span class="pun">:=</span><span class="pln"> ioutil</span><span class="pun">.</span><span class="typ">ReadDir</span><span class="pun">(</span><span class="str">"./"</span><span class="pun">)</span>
    <span class="kwd">if</span><span class="pln"> err </span><span class="pun">!=</span> <span class="kwd">nil</span> <span class="pun">{</span><span class="pln">
        log</span><span class="pun">.</span><span class="typ">Fatal</span><span class="pun">(</span><span class="pln">err</span><span class="pun">)</span>
    <span class="pun">}</span>

    <span class="kwd">for</span><span class="pln"> _</span><span class="pun">,</span><span class="pln"> f </span><span class="pun">:=</span><span class="pln"> range files </span><span class="pun">{</span><span class="pln">
            fmt</span><span class="pun">.</span><span class="typ">Println</span><span class="pun">(</span><span class="pln">f</span><span class="pun">.</span><span class="typ">Name</span><span class="pun">())</span>
    <span class="pun">}</span>
<span class="pun">}</span></code>

//stackoverflow.com/questions/14668850/list-directory-in-go