Add directories example
This commit is contained in:
parent
6eae6fe9b7
commit
447d77234f
@ -58,6 +58,7 @@ Reading Files
|
|||||||
Writing Files
|
Writing Files
|
||||||
Line Filters
|
Line Filters
|
||||||
File Paths
|
File Paths
|
||||||
|
Directories
|
||||||
Command-Line Arguments
|
Command-Line Arguments
|
||||||
Command-Line Flags
|
Command-Line Flags
|
||||||
Command-Line Subcommands
|
Command-Line Subcommands
|
||||||
|
77
examples/directories/directories.go
Normal file
77
examples/directories/directories.go
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
// Go has several useful functions for working with
|
||||||
|
// *directories* in the file system.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func check(e error) {
|
||||||
|
if e != nil {
|
||||||
|
panic(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
// Create a new sub-directory in the current working
|
||||||
|
// directory.
|
||||||
|
err := os.Mkdir("subdir", 0755)
|
||||||
|
check(err)
|
||||||
|
|
||||||
|
// When creating temporary directories, it's good
|
||||||
|
// practice to `defer` their removal. `os.RemoveAll`
|
||||||
|
// will delete a whole directory tree (similarly to
|
||||||
|
// `rm -rf`).
|
||||||
|
defer os.RemoveAll("subdir")
|
||||||
|
|
||||||
|
// Helper function to create a new empty file.
|
||||||
|
createEmptyFile := func(name string) {
|
||||||
|
d := []byte("")
|
||||||
|
check(ioutil.WriteFile(name, d, 0644))
|
||||||
|
}
|
||||||
|
|
||||||
|
createEmptyFile("subdir/file1")
|
||||||
|
|
||||||
|
// We can create a hierarchy of directories, including
|
||||||
|
// parents wiht `MkdirAll`. This is similar to the
|
||||||
|
// command-line `mkdir -p`.
|
||||||
|
err = os.MkdirAll("subdir/parent/child", 0755)
|
||||||
|
check(err)
|
||||||
|
|
||||||
|
createEmptyFile("subdir/parent/file2")
|
||||||
|
createEmptyFile("subdir/parent/file3")
|
||||||
|
createEmptyFile("subdir/parent/child/file4")
|
||||||
|
|
||||||
|
// `ReadDir` lists directory contents, returning a
|
||||||
|
// slice of `os.FileInfo` objects.
|
||||||
|
c, err := ioutil.ReadDir("subdir/parent")
|
||||||
|
check(err)
|
||||||
|
|
||||||
|
fmt.Println("Listing subdir/parent")
|
||||||
|
for _, entry := range c {
|
||||||
|
fmt.Println(entry.Name(), entry.IsDir())
|
||||||
|
}
|
||||||
|
|
||||||
|
// `Chdir` lets us change the current working directory,
|
||||||
|
// similarly to `cd`.
|
||||||
|
err = os.Chdir("subdir/parent/child")
|
||||||
|
check(err)
|
||||||
|
|
||||||
|
// Now we'll see the contents of "subdir/parent/child"
|
||||||
|
// when listing the *current* directory.
|
||||||
|
c, err = ioutil.ReadDir(".")
|
||||||
|
check(err)
|
||||||
|
|
||||||
|
fmt.Println("Listing subdir/parent/child")
|
||||||
|
for _, entry := range c {
|
||||||
|
fmt.Println(entry.Name(), entry.IsDir())
|
||||||
|
}
|
||||||
|
|
||||||
|
// `cd` back to where we started.
|
||||||
|
err = os.Chdir("../../..")
|
||||||
|
check(err)
|
||||||
|
}
|
2
examples/directories/directories.hash
Normal file
2
examples/directories/directories.hash
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
7b2eed223a00c8a84df582bd254a642c7a57dd9b
|
||||||
|
UnjBL6NmR8-
|
7
examples/directories/directories.sh
Normal file
7
examples/directories/directories.sh
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
$ go run directories.go
|
||||||
|
Listing subdir/parent
|
||||||
|
child true
|
||||||
|
file2 false
|
||||||
|
file3 false
|
||||||
|
Listing subdir/parent/child
|
||||||
|
file4 false
|
297
public/directories
Normal file
297
public/directories
Normal file
@ -0,0 +1,297 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>Go by Example: Directories</title>
|
||||||
|
<link rel=stylesheet href="site.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="example" id="directories">
|
||||||
|
<h2><a href="./">Go by Example</a>: Directories</h2>
|
||||||
|
|
||||||
|
<table>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>Go has several useful functions for working with
|
||||||
|
<em>directories</em> in the file system.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code empty leading">
|
||||||
|
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
<a href="http://play.golang.org/p/UnjBL6NmR8-">
|
||||||
|
<img title="Run code" class="run" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAPCAYAAADtc08vAAAABGdBTUEAANbY1E9YMgAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAJ1SURBVCjPY/j//z8DMu7o6GAAgpQgD9tLqcmJH4KDg14aaik/MtdXe2ZjY6OCrh6Fs2jRYmZ9Pd05M9uL/u9dPfU/CLS0dfxvKIz/X5Dg/z8pKdkGqwGpqakMUdExDHJSYqt37tjxf+qUSf9rc2P+79298/+RA3v+H1zV///o6r7/DrbWFQkJiQwxMTGoBjAxMTpKiQmuqMuP/f/xw/v/J0+f/W9tbvTfxVLn/8rJVf+v757z/96hRf8TQtxuCQmLMjk4OKAawMfDVWVvrvd85eTq/7tXTP6/e/XM/22lif9LCnL+b13Q/v/Kzln/L++c/X/7/Jb/VpYWuZFRUagGAAErEBtlxvi+vn944f9L26cDNcz6v21R9/8zm6aC2SBDbu+f/78kK+4/L79AO7oBYCAqxD/57JZp/y/tmPX/wrYZ/6+CbAayD6zs/78daBjIgPayFJAGG6wGAIFAcpjH/dv7F4ANABuya/b/Od3l/ye2V/+/tnv2/7ldxSANmrgMYGBhZg7fuagD7GyYIeeBrrqwdRrQgLn/l02sBGkwwWkAEAjV5EZ/vQV0LswAGAYZsLC3DKTBAJ8BzCkRni/uHFyIYcAtoNc6ypL/ANVIohigrKwMxqqqqgxMzKzM6VHeL+6iGQAKzDtAV5XlJv3n5uFLRTHgzZs3YPzz50+GwqJiPitD9Y8Pjy4BB+CNvfP+3wUmIpAhhckhr3X19LodHZ28UQxQU1MDYw0NDQYBAQEeoBOTK7JjP2xf3Pt/bkfB/4KkoDcKMmIL5OXlFerq6hhu3rzJgC8MwMDYxGSfm5vbVn9/f0cgVxAkpqioyFBfX49iAACbTAK+xT3CzgAAAABJRU5ErkJggg==" />
|
||||||
|
</a>
|
||||||
|
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||||
|
<span class="s">"fmt"</span>
|
||||||
|
<span class="s">"io/ioutil"</span>
|
||||||
|
<span class="s">"os"</span>
|
||||||
|
<span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">check</span><span class="p">(</span><span class="nx">e</span> <span class="kt">error</span><span class="p">)</span> <span class="p">{</span>
|
||||||
|
<span class="k">if</span> <span class="nx">e</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
|
||||||
|
<span class="nb">panic</span><span class="p">(</span><span class="nx">e</span><span class="p">)</span>
|
||||||
|
<span class="p">}</span>
|
||||||
|
<span class="p">}</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>Create a new sub-directory in the current working
|
||||||
|
directory.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">os</span><span class="p">.</span><span class="nx">Mkdir</span><span class="p">(</span><span class="s">"subdir"</span><span class="p">,</span> <span class="mo">0755</span><span class="p">)</span>
|
||||||
|
<span class="nx">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>When creating temporary directories, it’s good
|
||||||
|
practice to <code>defer</code> their removal. <code>os.RemoveAll</code>
|
||||||
|
will delete a whole directory tree (similarly to
|
||||||
|
<code>rm -rf</code>).</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="k">defer</span> <span class="nx">os</span><span class="p">.</span><span class="nx">RemoveAll</span><span class="p">(</span><span class="s">"subdir"</span><span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>Helper function to create a new empty file.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">createEmptyFile</span> <span class="o">:=</span> <span class="kd">func</span><span class="p">(</span><span class="nx">name</span> <span class="kt">string</span><span class="p">)</span> <span class="p">{</span>
|
||||||
|
<span class="nx">d</span> <span class="o">:=</span> <span class="p">[]</span><span class="nb">byte</span><span class="p">(</span><span class="s">""</span><span class="p">)</span>
|
||||||
|
<span class="nx">check</span><span class="p">(</span><span class="nx">ioutil</span><span class="p">.</span><span class="nx">WriteFile</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">d</span><span class="p">,</span> <span class="mo">0644</span><span class="p">))</span>
|
||||||
|
<span class="p">}</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">createEmptyFile</span><span class="p">(</span><span class="s">"subdir/file1"</span><span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>We can create a hierarchy of directories, including
|
||||||
|
parents wiht <code>MkdirAll</code>. This is similar to the
|
||||||
|
command-line <code>mkdir -p</code>.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">err</span> <span class="p">=</span> <span class="nx">os</span><span class="p">.</span><span class="nx">MkdirAll</span><span class="p">(</span><span class="s">"subdir/parent/child"</span><span class="p">,</span> <span class="mo">0755</span><span class="p">)</span>
|
||||||
|
<span class="nx">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">createEmptyFile</span><span class="p">(</span><span class="s">"subdir/parent/file2"</span><span class="p">)</span>
|
||||||
|
<span class="nx">createEmptyFile</span><span class="p">(</span><span class="s">"subdir/parent/file3"</span><span class="p">)</span>
|
||||||
|
<span class="nx">createEmptyFile</span><span class="p">(</span><span class="s">"subdir/parent/child/file4"</span><span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p><code>ReadDir</code> lists directory contents, returning a
|
||||||
|
slice of <code>os.FileInfo</code> objects.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">c</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">ioutil</span><span class="p">.</span><span class="nx">ReadDir</span><span class="p">(</span><span class="s">"subdir/parent"</span><span class="p">)</span>
|
||||||
|
<span class="nx">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"Listing subdir/parent"</span><span class="p">)</span>
|
||||||
|
<span class="k">for</span> <span class="nx">_</span><span class="p">,</span> <span class="nx">entry</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">c</span> <span class="p">{</span>
|
||||||
|
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">entry</span><span class="p">.</span><span class="nx">Name</span><span class="p">(),</span> <span class="nx">entry</span><span class="p">.</span><span class="nx">IsDir</span><span class="p">())</span>
|
||||||
|
<span class="p">}</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p><code>Chdir</code> lets us change the current working directory,
|
||||||
|
similarly to <code>cd</code>.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">err</span> <span class="p">=</span> <span class="nx">os</span><span class="p">.</span><span class="nx">Chdir</span><span class="p">(</span><span class="s">"subdir/parent/child"</span><span class="p">)</span>
|
||||||
|
<span class="nx">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p>Now we’ll see the contents of “subdir/parent/child”
|
||||||
|
when listing the <em>current</em> directory.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">c</span><span class="p">,</span> <span class="nx">err</span> <span class="p">=</span> <span class="nx">ioutil</span><span class="p">.</span><span class="nx">ReadDir</span><span class="p">(</span><span class="s">"."</span><span class="p">)</span>
|
||||||
|
<span class="nx">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code leading">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"Listing subdir/parent/child"</span><span class="p">)</span>
|
||||||
|
<span class="k">for</span> <span class="nx">_</span><span class="p">,</span> <span class="nx">entry</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">c</span> <span class="p">{</span>
|
||||||
|
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">entry</span><span class="p">.</span><span class="nx">Name</span><span class="p">(),</span> <span class="nx">entry</span><span class="p">.</span><span class="nx">IsDir</span><span class="p">())</span>
|
||||||
|
<span class="p">}</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
<p><code>cd</code> back to where we started.</p>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code">
|
||||||
|
|
||||||
|
<div class="highlight"><pre> <span class="nx">err</span> <span class="p">=</span> <span class="nx">os</span><span class="p">.</span><span class="nx">Chdir</span><span class="p">(</span><span class="s">"../../.."</span><span class="p">)</span>
|
||||||
|
<span class="nx">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
|
||||||
|
<span class="p">}</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<table>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td class="docs">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
<td class="code">
|
||||||
|
|
||||||
|
<div class="highlight"><pre><span class="gp">$</span> go run directories.go
|
||||||
|
<span class="go">Listing subdir/parent</span>
|
||||||
|
<span class="go">child true</span>
|
||||||
|
<span class="go">file2 false</span>
|
||||||
|
<span class="go">file3 false</span>
|
||||||
|
<span class="go">Listing subdir/parent/child</span>
|
||||||
|
<span class="go">file4 false</span>
|
||||||
|
</pre></div>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
</table>
|
||||||
|
|
||||||
|
|
||||||
|
<p class="next">
|
||||||
|
Next example: <a href="command-line-arguments">Command-Line Arguments</a>.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p class="footer">
|
||||||
|
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/directories">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -229,7 +229,7 @@ be made relative to base.</p>
|
|||||||
|
|
||||||
|
|
||||||
<p class="next">
|
<p class="next">
|
||||||
Next example: <a href="command-line-arguments">Command-Line Arguments</a>.
|
Next example: <a href="directories">Directories</a>.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p class="footer">
|
<p class="footer">
|
||||||
|
@ -143,6 +143,8 @@
|
|||||||
|
|
||||||
<li><a href="file-paths">File Paths</a></li>
|
<li><a href="file-paths">File Paths</a></li>
|
||||||
|
|
||||||
|
<li><a href="directories">Directories</a></li>
|
||||||
|
|
||||||
<li><a href="command-line-arguments">Command-Line Arguments</a></li>
|
<li><a href="command-line-arguments">Command-Line Arguments</a></li>
|
||||||
|
|
||||||
<li><a href="command-line-flags">Command-Line Flags</a></li>
|
<li><a href="command-line-flags">Command-Line Flags</a></li>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user