-
Notifications
You must be signed in to change notification settings - Fork 8
/
directories.html
341 lines (267 loc) · 16.9 KB
/
directories.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example 中文版: 目录</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'file-paths';
}
if (e.key == "ArrowRight") {
window.location.href = 'temporary-files-and-directories';
}
}
</script>
<body>
<div class="example" id="directories">
<h2><a href="./">Go by Example 中文版</a>: 目录</h2>
<table>
<tr>
<td class="docs">
<p>对于操作文件系统中的 <em>目录</em> ,Go 提供了几个非常有用的函数。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="https://play.studygolang.com/p/zlr179K_a_a"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kn">import</span> <span class="p">(</span>
<span class="s">"fmt"</span>
<span class="s">"os"</span>
<span class="s">"path/filepath"</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">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>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>在当前工作目录下,创建一个子目录。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">err</span> <span class="o">:=</span> <span class="nx">os</span><span class="p">.</span><span class="nf">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="nf">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>创建这个临时目录后,一个好习惯是:使用 <code>defer</code> 删除这个目录。
<code>os.RemoveAll</code> 会删除整个目录(类似于 <code>rm -rf</code>)。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="k">defer</span> <span class="nx">os</span><span class="p">.</span><span class="nf">RemoveAll</span><span class="p">(</span><span class="s">"subdir"</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>一个用于创建临时文件的帮助函数。</p>
</td>
<td class="code leading">
<pre class="chroma">
<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="nf">check</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nf">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>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"> <span class="nf">createEmptyFile</span><span class="p">(</span><span class="s">"subdir/file1"</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>我们还可以创建一个有层级的目录,使用 <code>MkdirAll</code> 函数,并包含其父目录。
这个类似于命令 <code>mkdir -p</code>。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">err</span> <span class="p">=</span> <span class="nx">os</span><span class="p">.</span><span class="nf">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="nf">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"> <span class="nf">createEmptyFile</span><span class="p">(</span><span class="s">"subdir/parent/file2"</span><span class="p">)</span>
<span class="nf">createEmptyFile</span><span class="p">(</span><span class="s">"subdir/parent/file3"</span><span class="p">)</span>
<span class="nf">createEmptyFile</span><span class="p">(</span><span class="s">"subdir/parent/child/file4"</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>ReadDir</code> 列出目录的内容,返回一个 <code>os.DirEntry</code> 类型的切片对象。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">c</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">os</span><span class="p">.</span><span class="nf">ReadDir</span><span class="p">(</span><span class="s">"subdir/parent"</span><span class="p">)</span>
<span class="nf">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"> <span class="nx">fmt</span><span class="p">.</span><span class="nf">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="nf">Println</span><span class="p">(</span><span class="s">" "</span><span class="p">,</span> <span class="nx">entry</span><span class="p">.</span><span class="nf">Name</span><span class="p">(),</span> <span class="nx">entry</span><span class="p">.</span><span class="nf">IsDir</span><span class="p">())</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>Chdir</code> 可以修改当前工作目录,类似于 <code>cd</code>。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">err</span> <span class="p">=</span> <span class="nx">os</span><span class="p">.</span><span class="nf">Chdir</span><span class="p">(</span><span class="s">"subdir/parent/child"</span><span class="p">)</span>
<span class="nf">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>当我们列出 <em>当前</em> 目录,就可以看到 <code>subdir/parent/child</code> 的内容了。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">c</span><span class="p">,</span> <span class="nx">err</span> <span class="p">=</span> <span class="nx">os</span><span class="p">.</span><span class="nf">ReadDir</span><span class="p">(</span><span class="s">"."</span><span class="p">)</span>
<span class="nf">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"> <span class="nx">fmt</span><span class="p">.</span><span class="nf">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="nf">Println</span><span class="p">(</span><span class="s">" "</span><span class="p">,</span> <span class="nx">entry</span><span class="p">.</span><span class="nf">Name</span><span class="p">(),</span> <span class="nx">entry</span><span class="p">.</span><span class="nf">IsDir</span><span class="p">())</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>cd</code> 回到最开始的地方。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">err</span> <span class="p">=</span> <span class="nx">os</span><span class="p">.</span><span class="nf">Chdir</span><span class="p">(</span><span class="s">"../../.."</span><span class="p">)</span>
<span class="nf">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>当然,我们也可以遍历一个目录及其所有子目录。
<code>Walk</code> 接受一个路径和回调函数,用于处理访问到的每个目录和文件。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"Visiting subdir"</span><span class="p">)</span>
<span class="nx">err</span> <span class="p">=</span> <span class="nx">filepath</span><span class="p">.</span><span class="nf">Walk</span><span class="p">(</span><span class="s">"subdir"</span><span class="p">,</span> <span class="nx">visit</span><span class="p">)</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>filepath.Walk</code> 遍历访问到每一个目录和文件后,都会调用 <code>visit</code>。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="kd">func</span> <span class="nf">visit</span><span class="p">(</span><span class="nx">p</span> <span class="kt">string</span><span class="p">,</span> <span class="nx">info</span> <span class="nx">os</span><span class="p">.</span><span class="nx">FileInfo</span><span class="p">,</span> <span class="nx">err</span> <span class="kt">error</span><span class="p">)</span> <span class="kt">error</span> <span class="p">{</span>
<span class="k">if</span> <span class="nx">err</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">err</span>
<span class="p">}</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">" "</span><span class="p">,</span> <span class="nx">p</span><span class="p">,</span> <span class="nx">info</span><span class="p">.</span><span class="nf">IsDir</span><span class="p">())</span>
<span class="k">return</span> <span class="kc">nil</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
</td>
<td class="code">
<pre class="chroma"><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><span class="go">Visiting subdir
</span><span class="go"> subdir true
</span><span class="go"> subdir/file1 false
</span><span class="go"> subdir/parent true
</span><span class="go"> subdir/parent/child true
</span><span class="go"> subdir/parent/child/file4 false
</span><span class="go"> subdir/parent/file2 false
</span><span class="go"> subdir/parent/file3 false</span></pre>
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="temporary-files-and-directories">临时文件和目录</a>
</p>
<p class="footer">
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 和<a href="https://eli.thegreenplace.net">Eli Bendersky</a>编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"os\"\u000A \"path/filepath\"\u000A)\u000A');codeLines.push('func check(e error) {\u000A if e !\u003D nil {\u000A panic(e)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' err :\u003D os.Mkdir(\"subdir\", 0755)\u000A check(err)\u000A');codeLines.push(' defer os.RemoveAll(\"subdir\")\u000A');codeLines.push(' createEmptyFile :\u003D func(name string) {\u000A d :\u003D []byte(\"\")\u000A check(os.WriteFile(name, d, 0644))\u000A }\u000A');codeLines.push(' createEmptyFile(\"subdir/file1\")\u000A');codeLines.push(' err \u003D os.MkdirAll(\"subdir/parent/child\", 0755)\u000A check(err)\u000A');codeLines.push(' createEmptyFile(\"subdir/parent/file2\")\u000A createEmptyFile(\"subdir/parent/file3\")\u000A createEmptyFile(\"subdir/parent/child/file4\")\u000A');codeLines.push(' c, err :\u003D os.ReadDir(\"subdir/parent\")\u000A check(err)\u000A');codeLines.push(' fmt.Println(\"Listing subdir/parent\")\u000A for _, entry :\u003D range c {\u000A fmt.Println(\" \", entry.Name(), entry.IsDir())\u000A }\u000A');codeLines.push(' err \u003D os.Chdir(\"subdir/parent/child\")\u000A check(err)\u000A');codeLines.push(' c, err \u003D os.ReadDir(\".\")\u000A check(err)\u000A');codeLines.push(' fmt.Println(\"Listing subdir/parent/child\")\u000A for _, entry :\u003D range c {\u000A fmt.Println(\" \", entry.Name(), entry.IsDir())\u000A }\u000A');codeLines.push(' err \u003D os.Chdir(\"../../..\")\u000A check(err)\u000A');codeLines.push(' fmt.Println(\"Visiting subdir\")\u000A err \u003D filepath.Walk(\"subdir\", visit)\u000A}\u000A');codeLines.push('func visit(p string, info os.FileInfo, err error) error {\u000A if err !\u003D nil {\u000A return err\u000A }\u000A fmt.Println(\" \", p, info.IsDir())\u000A return nil\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>