-
Notifications
You must be signed in to change notification settings - Fork 8
/
temporary-files-and-directories.html
231 lines (178 loc) · 10.5 KB
/
temporary-files-and-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
<!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 = 'directories';
}
if (e.key == "ArrowRight") {
window.location.href = 'testing-and-benchmarking';
}
}
</script>
<body>
<div class="example" id="temporary-files-and-directories">
<h2><a href="./">Go by Example 中文版</a>: 临时文件和目录</h2>
<table>
<tr>
<td class="docs">
<p>在程序运行时,我们经常创建一些运行时用到,程序结束后就不再使用的数据。
<em>临时目录和文件</em> 对于上面的情况很有用,因为它不会随着时间的推移而污染文件系统。</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/3xju207mqQ3"><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>创建临时文件最简单的方法是调用 <code>os.CreateTemp</code> 函数。
它会创建并打开文件,我们可以对文件进行读写。
函数的第一个参数传 <code>""</code>,<code>os.CreateTemp</code> 会在操作系统的默认位置下创建该文件。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">f</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">CreateTemp</span><span class="p">(</span><span class="s">""</span><span class="p">,</span> <span class="s">"sample"</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>os.CreateTemp</code> 函数的第二个参数作为前缀,
剩余的部分会自动生成,以确保并发调用时,生成不重复的文件名。
在类 Unix 操作系统下,临时目录一般是 <code>/tmp</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">"Temp file name:"</span><span class="p">,</span> <span class="nx">f</span><span class="p">.</span><span class="nf">Name</span><span class="p">())</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>defer 删除该文件。
尽管操作系统会自动在某个时间清理临时文件,但手动清理是一个好习惯。</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">Remove</span><span class="p">(</span><span class="nx">f</span><span class="p">.</span><span class="nf">Name</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">_</span><span class="p">,</span> <span class="nx">err</span> <span class="p">=</span> <span class="nx">f</span><span class="p">.</span><span class="nf">Write</span><span class="p">([]</span><span class="kt">byte</span><span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</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>os.MkdirTemp</code> 的参数与 <code>CreateTemp</code> 相同,
但是它返回的是一个 <em>目录名</em> ,而不是一个打开的文件。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">dname</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">MkdirTemp</span><span class="p">(</span><span class="s">""</span><span class="p">,</span> <span class="s">"sampledir"</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">"Temp dir name:"</span><span class="p">,</span> <span class="nx">dname</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</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="nx">dname</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>现在,我们可以通过拼接临时目录和临时文件合成完整的临时文件路径,并写入数据。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="nx">fname</span> <span class="o">:=</span> <span class="nx">filepath</span><span class="p">.</span><span class="nf">Join</span><span class="p">(</span><span class="nx">dname</span><span class="p">,</span> <span class="s">"file1"</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">WriteFile</span><span class="p">(</span><span class="nx">fname</span><span class="p">,</span> <span class="p">[]</span><span class="kt">byte</span><span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">},</span> <span class="mo">0666</span><span class="p">)</span>
<span class="nf">check</span><span class="p">(</span><span class="nx">err</span><span class="p">)</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 temporary-files-and-directories.go
<span class="go">Temp file name: /tmp/sample610887201
</span><span class="go">Temp dir name: /tmp/sampledir898854668</span></pre>
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="testing-and-benchmarking">单元测试和基准测试</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(' f, err :\u003D os.CreateTemp(\"\", \"sample\")\u000A check(err)\u000A');codeLines.push(' fmt.Println(\"Temp file name:\", f.Name())\u000A');codeLines.push(' defer os.Remove(f.Name())\u000A');codeLines.push(' _, err \u003D f.Write([]byte{1, 2, 3, 4})\u000A check(err)\u000A');codeLines.push(' dname, err :\u003D os.MkdirTemp(\"\", \"sampledir\")\u000A fmt.Println(\"Temp dir name:\", dname)\u000A');codeLines.push(' defer os.RemoveAll(dname)\u000A');codeLines.push(' fname :\u003D filepath.Join(dname, \"file1\")\u000A err \u003D os.WriteFile(fname, []byte{1, 2}, 0666)\u000A check(err)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>