-
Notifications
You must be signed in to change notification settings - Fork 8
/
string-formatting.html
444 lines (346 loc) · 18.9 KB
/
string-formatting.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<!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 = 'string-functions';
}
if (e.key == "ArrowRight") {
window.location.href = 'text-templates';
}
}
</script>
<body>
<div class="example" id="string-formatting">
<h2><a href="./">Go by Example 中文版</a>: 字符串格式化</h2>
<table>
<tr>
<td class="docs">
<p>Go 在传统的 <code>printf</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/XY3ad4TXm3J"><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="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">type</span> <span class="nx">point</span> <span class="kd">struct</span> <span class="p">{</span>
<span class="nx">x</span><span class="p">,</span> <span class="nx">y</span> <span class="kt">int</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>Go 提供了一些用于格式化常规值的打印“动词”。
例如,这样打印 <code>point</code> 结构体的实例。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">p</span> <span class="o">:=</span> <span class="nx">point</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="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"struct1: %v\n"</span><span class="p">,</span> <span class="nx">p</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>如果值是一个结构体,<code>%+v</code> 的格式化输出内容将包括结构体的字段名。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"struct2: %+v\n"</span><span class="p">,</span> <span class="nx">p</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>%#v</code> 根据 Go 语法输出值,即会产生该值的源码片段。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"struct3: %#v\n"</span><span class="p">,</span> <span class="nx">p</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>需要打印值的类型,使用 <code>%T</code>。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"type: %T\n"</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">Printf</span><span class="p">(</span><span class="s">"bool: %t\n"</span><span class="p">,</span> <span class="kc">true</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>格式化整型数有多种方式,使用 <code>%d</code> 进行标准的十进制格式化。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"int: %d\n"</span><span class="p">,</span> <span class="mi">123</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">Printf</span><span class="p">(</span><span class="s">"bin: %b\n"</span><span class="p">,</span> <span class="mi">14</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">Printf</span><span class="p">(</span><span class="s">"char: %c\n"</span><span class="p">,</span> <span class="mi">33</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>%x</code> 提供了十六进制编码。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"hex: %x\n"</span><span class="p">,</span> <span class="mi">456</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>同样的,也为浮点型提供了多种格式化选项。
使用 <code>%f</code> 进行最基本的十进制格式化。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"float1: %f\n"</span><span class="p">,</span> <span class="mf">78.9</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>%e</code> 和 <code>%E</code> 将浮点型格式化为(稍微有一点不同的)科学记数法表示形式。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"float2: %e\n"</span><span class="p">,</span> <span class="mf">123400000.0</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"float3: %E\n"</span><span class="p">,</span> <span class="mf">123400000.0</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>使用 <code>%s</code> 进行基本的字符串输出。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"str1: %s\n"</span><span class="p">,</span> <span class="s">"\"string\""</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>像 Go 源代码中那样带有双引号的输出,使用 <code>%q</code>。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"str2: %q\n"</span><span class="p">,</span> <span class="s">"\"string\""</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>和上面的整型数一样,<code>%x</code> 输出使用 base-16 编码的字符串,
每个字节使用 2 个字符表示。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"str3: %x\n"</span><span class="p">,</span> <span class="s">"hex this"</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>要输出一个指针的值,使用 <code>%p</code>。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"pointer: %p\n"</span><span class="p">,</span> <span class="o">&</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">Printf</span><span class="p">(</span><span class="s">"width1: |%6d|%6d|\n"</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">345</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>你也可以指定浮点型的输出宽度,同时也可以通过 <code>宽度.精度</code> 的语法来指定输出的精度。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"width2: |%6.2f|%6.2f|\n"</span><span class="p">,</span> <span class="mf">1.2</span><span class="p">,</span> <span class="mf">3.45</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>要左对齐,使用 <code>-</code> 标志。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"width3: |%-6.2f|%-6.2f|\n"</span><span class="p">,</span> <span class="mf">1.2</span><span class="p">,</span> <span class="mf">3.45</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">Printf</span><span class="p">(</span><span class="s">"width4: |%6s|%6s|\n"</span><span class="p">,</span> <span class="s">"foo"</span><span class="p">,</span> <span class="s">"b"</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>要左对齐,和数字一样,使用 <code>-</code> 标志。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"width5: |%-6s|%-6s|\n"</span><span class="p">,</span> <span class="s">"foo"</span><span class="p">,</span> <span class="s">"b"</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>到目前为止,我们已经看过 <code>Printf</code> 了,
它通过 <code>os.Stdout</code> 输出格式化的字符串。
<code>Sprintf</code> 则格式化并返回一个字符串而没有任何输出。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">s</span> <span class="o">:=</span> <span class="nx">fmt</span><span class="p">.</span><span class="nf">Sprintf</span><span class="p">(</span><span class="s">"sprintf: a %s"</span><span class="p">,</span> <span class="s">"string"</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">s</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>你可以使用 <code>Fprintf</code> 来格式化并输出到 <code>io.Writers</code> 而不是 <code>os.Stdout</code>。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Fprintf</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Stderr</span><span class="p">,</span> <span class="s">"io: an %s\n"</span><span class="p">,</span> <span class="s">"error"</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 string-formatting.go
<span class="go">struct1: {1 2}
</span><span class="go">struct2: {x:1 y:2}
</span><span class="go">struct3: main.point{x:1, y:2}
</span><span class="go">type: main.point
</span><span class="go">bool: true
</span><span class="go">int: 123
</span><span class="go">bin: 1110
</span><span class="go">char: !
</span><span class="go">hex: 1c8
</span><span class="go">float1: 78.900000
</span><span class="go">float2: 1.234000e+08
</span><span class="go">float3: 1.234000E+08
</span><span class="go">str1: "string"
</span><span class="go">str2: "\"string\""
</span><span class="go">str3: 6865782074686973
</span><span class="go">pointer: 0xc0000ba000
</span><span class="go">width1: | 12| 345|
</span><span class="go">width2: | 1.20| 3.45|
</span><span class="go">width3: |1.20 |3.45 |
</span><span class="go">width4: | foo| b|
</span><span class="go">width5: |foo |b |
</span><span class="go">sprintf: a string
</span><span class="go">io: an error</span></pre>
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="text-templates">文本模板</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)\u000A');codeLines.push('type point struct {\u000A x, y int\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' p :\u003D point{1, 2}\u000A fmt.Printf(\"struct1: %v\\n\", p)\u000A');codeLines.push(' fmt.Printf(\"struct2: %+v\\n\", p)\u000A');codeLines.push(' fmt.Printf(\"struct3: %#v\\n\", p)\u000A');codeLines.push(' fmt.Printf(\"type: %T\\n\", p)\u000A');codeLines.push(' fmt.Printf(\"bool: %t\\n\", true)\u000A');codeLines.push(' fmt.Printf(\"int: %d\\n\", 123)\u000A');codeLines.push(' fmt.Printf(\"bin: %b\\n\", 14)\u000A');codeLines.push(' fmt.Printf(\"char: %c\\n\", 33)\u000A');codeLines.push(' fmt.Printf(\"hex: %x\\n\", 456)\u000A');codeLines.push(' fmt.Printf(\"float1: %f\\n\", 78.9)\u000A');codeLines.push(' fmt.Printf(\"float2: %e\\n\", 123400000.0)\u000A fmt.Printf(\"float3: %E\\n\", 123400000.0)\u000A');codeLines.push(' fmt.Printf(\"str1: %s\\n\", \"\\\"string\\\"\")\u000A');codeLines.push(' fmt.Printf(\"str2: %q\\n\", \"\\\"string\\\"\")\u000A');codeLines.push(' fmt.Printf(\"str3: %x\\n\", \"hex this\")\u000A');codeLines.push(' fmt.Printf(\"pointer: %p\\n\", \u0026p)\u000A');codeLines.push(' fmt.Printf(\"width1: |%6d|%6d|\\n\", 12, 345)\u000A');codeLines.push(' fmt.Printf(\"width2: |%6.2f|%6.2f|\\n\", 1.2, 3.45)\u000A');codeLines.push(' fmt.Printf(\"width3: |%-6.2f|%-6.2f|\\n\", 1.2, 3.45)\u000A');codeLines.push(' fmt.Printf(\"width4: |%6s|%6s|\\n\", \"foo\", \"b\")\u000A');codeLines.push(' fmt.Printf(\"width5: |%-6s|%-6s|\\n\", \"foo\", \"b\")\u000A');codeLines.push(' s :\u003D fmt.Sprintf(\"sprintf: a %s\", \"string\")\u000A fmt.Println(s)\u000A');codeLines.push(' fmt.Fprintf(os.Stderr, \"io: an %s\\n\", \"error\")\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>