-
Notifications
You must be signed in to change notification settings - Fork 8
/
file-paths.html
254 lines (198 loc) · 12.2 KB
/
file-paths.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
<!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 = 'line-filters';
}
if (e.key == "ArrowRight") {
window.location.href = 'directories';
}
}
</script>
<body>
<div class="example" id="file-paths">
<h2><a href="./">Go by Example 中文版</a>: 文件路径</h2>
<table>
<tr>
<td class="docs">
<p><code>filepath</code> 包为 <em>文件路径</em> ,提供了方便的跨操作系统的解析和构建函数;
比如:Linux 下的 <code>dir/file</code> 和 Windows 下的 <code>dir\file</code> 。</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/9Spis9glc46"><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">"path/filepath"</span>
<span class="s">"strings"</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>Join</code> 来构建可移植(跨操作系统)的路径。
它接收任意数量的参数,并参照传入顺序构造一个对应层次结构的路径。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">p</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="s">"dir1"</span><span class="p">,</span> <span class="s">"dir2"</span><span class="p">,</span> <span class="s">"filename"</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">"p:"</span><span class="p">,</span> <span class="nx">p</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>您应该总是使用 <code>Join</code> 代替手动拼接 <code>/</code> 和 <code>\</code>。
除了可移植性,<code>Join</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="nx">filepath</span><span class="p">.</span><span class="nf">Join</span><span class="p">(</span><span class="s">"dir1//"</span><span class="p">,</span> <span class="s">"filename"</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="nx">filepath</span><span class="p">.</span><span class="nf">Join</span><span class="p">(</span><span class="s">"dir1/../dir1"</span><span class="p">,</span> <span class="s">"filename"</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>Dir</code> 和 <code>Base</code> 可以被用于分割路径中的目录和文件。
此外,<code>Split</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">"Dir(p):"</span><span class="p">,</span> <span class="nx">filepath</span><span class="p">.</span><span class="nf">Dir</span><span class="p">(</span><span class="nx">p</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">"Base(p):"</span><span class="p">,</span> <span class="nx">filepath</span><span class="p">.</span><span class="nf">Base</span><span class="p">(</span><span class="nx">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">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">filepath</span><span class="p">.</span><span class="nf">IsAbs</span><span class="p">(</span><span class="s">"dir/file"</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="nx">filepath</span><span class="p">.</span><span class="nf">IsAbs</span><span class="p">(</span><span class="s">"/dir/file"</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"> <span class="nx">filename</span> <span class="o">:=</span> <span class="s">"config.json"</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>某些文件名包含了扩展名(文件类型)。
我们可以用 <code>Ext</code> 将扩展名分割出来。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">ext</span> <span class="o">:=</span> <span class="nx">filepath</span><span class="p">.</span><span class="nf">Ext</span><span class="p">(</span><span class="nx">filename</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="nx">ext</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>想获取文件名清除扩展名后的值,请使用 <code>strings.TrmSuffix</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="nx">strings</span><span class="p">.</span><span class="nf">TrimSuffix</span><span class="p">(</span><span class="nx">filename</span><span class="p">,</span> <span class="nx">ext</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>Rel</code> 寻找 <code>basepath</code> 与 <code>targpath</code> 之间的相对路径。
如果相对路径不存在,则返回错误。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">rel</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">filepath</span><span class="p">.</span><span class="nf">Rel</span><span class="p">(</span><span class="s">"a/b"</span><span class="p">,</span> <span class="s">"a/b/t/file"</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="nb">panic</span><span class="p">(</span><span class="nx">err</span><span class="p">)</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="nx">rel</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code">
<pre class="chroma"> <span class="nx">rel</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">Rel</span><span class="p">(</span><span class="s">"a/b"</span><span class="p">,</span> <span class="s">"a/c/t/file"</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="nb">panic</span><span class="p">(</span><span class="nx">err</span><span class="p">)</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="nx">rel</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 file-paths.go
<span class="go">p: dir1/dir2/filename
</span><span class="go">dir1/filename
</span><span class="go">dir1/filename
</span><span class="go">Dir(p): dir1/dir2
</span><span class="go">Base(p): filename
</span><span class="go">false
</span><span class="go">true
</span><span class="go">.json
</span><span class="go">config
</span><span class="go">t/file
</span><span class="go">../c/t/file</span></pre>
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="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 \"path/filepath\"\u000A \"strings\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' p :\u003D filepath.Join(\"dir1\", \"dir2\", \"filename\")\u000A fmt.Println(\"p:\", p)\u000A');codeLines.push(' fmt.Println(filepath.Join(\"dir1//\", \"filename\"))\u000A fmt.Println(filepath.Join(\"dir1/../dir1\", \"filename\"))\u000A');codeLines.push(' fmt.Println(\"Dir(p):\", filepath.Dir(p))\u000A fmt.Println(\"Base(p):\", filepath.Base(p))\u000A');codeLines.push(' fmt.Println(filepath.IsAbs(\"dir/file\"))\u000A fmt.Println(filepath.IsAbs(\"/dir/file\"))\u000A');codeLines.push(' filename :\u003D \"config.json\"\u000A');codeLines.push(' ext :\u003D filepath.Ext(filename)\u000A fmt.Println(ext)\u000A');codeLines.push(' fmt.Println(strings.TrimSuffix(filename, ext))\u000A');codeLines.push(' rel, err :\u003D filepath.Rel(\"a/b\", \"a/b/t/file\")\u000A if err !\u003D nil {\u000A panic(err)\u000A }\u000A fmt.Println(rel)\u000A');codeLines.push(' rel, err \u003D filepath.Rel(\"a/b\", \"a/c/t/file\")\u000A if err !\u003D nil {\u000A panic(err)\u000A }\u000A fmt.Println(rel)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>