Source file
src/strconv/example_test.go
Documentation: strconv
1
2
3
4
5 package strconv_test
6
7 import (
8 "fmt"
9 "log"
10 "strconv"
11 )
12
13 func ExampleAppendBool() {
14 b := []byte("bool:")
15 b = strconv.AppendBool(b, true)
16 fmt.Println(string(b))
17
18
19
20 }
21
22 func ExampleAppendFloat() {
23 b32 := []byte("float32:")
24 b32 = strconv.AppendFloat(b32, 3.1415926535, 'E', -1, 32)
25 fmt.Println(string(b32))
26
27 b64 := []byte("float64:")
28 b64 = strconv.AppendFloat(b64, 3.1415926535, 'E', -1, 64)
29 fmt.Println(string(b64))
30
31
32
33
34 }
35
36 func ExampleAppendInt() {
37 b10 := []byte("int (base 10):")
38 b10 = strconv.AppendInt(b10, -42, 10)
39 fmt.Println(string(b10))
40
41 b16 := []byte("int (base 16):")
42 b16 = strconv.AppendInt(b16, -42, 16)
43 fmt.Println(string(b16))
44
45
46
47
48 }
49
50 func ExampleAppendQuote() {
51 b := []byte("quote:")
52 b = strconv.AppendQuote(b, `"Fran & Freddie's Diner"`)
53 fmt.Println(string(b))
54
55
56
57 }
58
59 func ExampleAppendQuoteRune() {
60 b := []byte("rune:")
61 b = strconv.AppendQuoteRune(b, '☺')
62 fmt.Println(string(b))
63
64
65
66 }
67
68 func ExampleAppendQuoteRuneToASCII() {
69 b := []byte("rune (ascii):")
70 b = strconv.AppendQuoteRuneToASCII(b, '☺')
71 fmt.Println(string(b))
72
73
74
75 }
76
77 func ExampleAppendQuoteToASCII() {
78 b := []byte("quote (ascii):")
79 b = strconv.AppendQuoteToASCII(b, `"Fran & Freddie's Diner"`)
80 fmt.Println(string(b))
81
82
83
84 }
85
86 func ExampleAppendUint() {
87 b10 := []byte("uint (base 10):")
88 b10 = strconv.AppendUint(b10, 42, 10)
89 fmt.Println(string(b10))
90
91 b16 := []byte("uint (base 16):")
92 b16 = strconv.AppendUint(b16, 42, 16)
93 fmt.Println(string(b16))
94
95
96
97
98 }
99
100 func ExampleAtoi() {
101 v := "10"
102 if s, err := strconv.Atoi(v); err == nil {
103 fmt.Printf("%T, %v", s, s)
104 }
105
106
107
108 }
109
110 func ExampleCanBackquote() {
111 fmt.Println(strconv.CanBackquote("Fran & Freddie's Diner ☺"))
112 fmt.Println(strconv.CanBackquote("`can't backquote this`"))
113
114
115
116
117 }
118
119 func ExampleFormatBool() {
120 v := true
121 s := strconv.FormatBool(v)
122 fmt.Printf("%T, %v\n", s, s)
123
124
125
126 }
127
128 func ExampleFormatFloat() {
129 v := 3.1415926535
130
131 s32 := strconv.FormatFloat(v, 'E', -1, 32)
132 fmt.Printf("%T, %v\n", s32, s32)
133
134 s64 := strconv.FormatFloat(v, 'E', -1, 64)
135 fmt.Printf("%T, %v\n", s64, s64)
136
137
138 fmt64 := strconv.FormatFloat(v, 'g', -1, 64)
139 fmt.Printf("%T, %v\n", fmt64, fmt64)
140
141
142
143
144
145 }
146
147 func ExampleFormatInt() {
148 v := int64(-42)
149
150 s10 := strconv.FormatInt(v, 10)
151 fmt.Printf("%T, %v\n", s10, s10)
152
153 s16 := strconv.FormatInt(v, 16)
154 fmt.Printf("%T, %v\n", s16, s16)
155
156
157
158
159 }
160
161 func ExampleFormatUint() {
162 v := uint64(42)
163
164 s10 := strconv.FormatUint(v, 10)
165 fmt.Printf("%T, %v\n", s10, s10)
166
167 s16 := strconv.FormatUint(v, 16)
168 fmt.Printf("%T, %v\n", s16, s16)
169
170
171
172
173 }
174
175 func ExampleIsGraphic() {
176 shamrock := strconv.IsGraphic('☘')
177 fmt.Println(shamrock)
178
179 a := strconv.IsGraphic('a')
180 fmt.Println(a)
181
182 bel := strconv.IsGraphic('\007')
183 fmt.Println(bel)
184
185
186
187
188
189 }
190
191 func ExampleIsPrint() {
192 c := strconv.IsPrint('\u263a')
193 fmt.Println(c)
194
195 bel := strconv.IsPrint('\007')
196 fmt.Println(bel)
197
198
199
200
201 }
202
203 func ExampleItoa() {
204 i := 10
205 s := strconv.Itoa(i)
206 fmt.Printf("%T, %v\n", s, s)
207
208
209
210 }
211
212 func ExampleParseBool() {
213 v := "true"
214 if s, err := strconv.ParseBool(v); err == nil {
215 fmt.Printf("%T, %v\n", s, s)
216 }
217
218
219
220 }
221
222 func ExampleParseFloat() {
223 v := "3.1415926535"
224 if s, err := strconv.ParseFloat(v, 32); err == nil {
225 fmt.Printf("%T, %v\n", s, s)
226 }
227 if s, err := strconv.ParseFloat(v, 64); err == nil {
228 fmt.Printf("%T, %v\n", s, s)
229 }
230 if s, err := strconv.ParseFloat("NaN", 32); err == nil {
231 fmt.Printf("%T, %v\n", s, s)
232 }
233
234 if s, err := strconv.ParseFloat("nan", 32); err == nil {
235 fmt.Printf("%T, %v\n", s, s)
236 }
237 if s, err := strconv.ParseFloat("inf", 32); err == nil {
238 fmt.Printf("%T, %v\n", s, s)
239 }
240 if s, err := strconv.ParseFloat("+Inf", 32); err == nil {
241 fmt.Printf("%T, %v\n", s, s)
242 }
243 if s, err := strconv.ParseFloat("-Inf", 32); err == nil {
244 fmt.Printf("%T, %v\n", s, s)
245 }
246 if s, err := strconv.ParseFloat("-0", 32); err == nil {
247 fmt.Printf("%T, %v\n", s, s)
248 }
249 if s, err := strconv.ParseFloat("+0", 32); err == nil {
250 fmt.Printf("%T, %v\n", s, s)
251 }
252
253
254
255
256
257
258
259
260
261
262
263 }
264
265 func ExampleParseInt() {
266 v32 := "-354634382"
267 if s, err := strconv.ParseInt(v32, 10, 32); err == nil {
268 fmt.Printf("%T, %v\n", s, s)
269 }
270 if s, err := strconv.ParseInt(v32, 16, 32); err == nil {
271 fmt.Printf("%T, %v\n", s, s)
272 }
273
274 v64 := "-3546343826724305832"
275 if s, err := strconv.ParseInt(v64, 10, 64); err == nil {
276 fmt.Printf("%T, %v\n", s, s)
277 }
278 if s, err := strconv.ParseInt(v64, 16, 64); err == nil {
279 fmt.Printf("%T, %v\n", s, s)
280 }
281
282
283
284
285 }
286
287 func ExampleParseUint() {
288 v := "42"
289 if s, err := strconv.ParseUint(v, 10, 32); err == nil {
290 fmt.Printf("%T, %v\n", s, s)
291 }
292 if s, err := strconv.ParseUint(v, 10, 64); err == nil {
293 fmt.Printf("%T, %v\n", s, s)
294 }
295
296
297
298
299 }
300
301 func ExampleQuote() {
302
303 s := strconv.Quote(`"Fran & Freddie's Diner ☺"`)
304 fmt.Println(s)
305
306
307
308 }
309
310 func ExampleQuoteRune() {
311 s := strconv.QuoteRune('☺')
312 fmt.Println(s)
313
314
315
316 }
317
318 func ExampleQuoteRuneToASCII() {
319 s := strconv.QuoteRuneToASCII('☺')
320 fmt.Println(s)
321
322
323
324 }
325
326 func ExampleQuoteRuneToGraphic() {
327 s := strconv.QuoteRuneToGraphic('☺')
328 fmt.Println(s)
329
330 s = strconv.QuoteRuneToGraphic('\u263a')
331 fmt.Println(s)
332
333 s = strconv.QuoteRuneToGraphic('\u000a')
334 fmt.Println(s)
335
336 s = strconv.QuoteRuneToGraphic(' ')
337 fmt.Println(s)
338
339
340
341
342
343
344 }
345
346 func ExampleQuoteToASCII() {
347
348 s := strconv.QuoteToASCII(`"Fran & Freddie's Diner ☺"`)
349 fmt.Println(s)
350
351
352
353 }
354
355 func ExampleQuoteToGraphic() {
356 s := strconv.QuoteToGraphic("☺")
357 fmt.Println(s)
358
359
360 s = strconv.QuoteToGraphic("This is a \u263a \u000a")
361 fmt.Println(s)
362
363 s = strconv.QuoteToGraphic(`" This is a ☺ \n "`)
364 fmt.Println(s)
365
366
367
368
369
370 }
371
372 func ExampleQuotedPrefix() {
373 s, err := strconv.QuotedPrefix("not a quoted string")
374 fmt.Printf("%q, %v\n", s, err)
375 s, err = strconv.QuotedPrefix("\"double-quoted string\" with trailing text")
376 fmt.Printf("%q, %v\n", s, err)
377 s, err = strconv.QuotedPrefix("`or backquoted` with more trailing text")
378 fmt.Printf("%q, %v\n", s, err)
379 s, err = strconv.QuotedPrefix("'\u263a' is also okay")
380 fmt.Printf("%q, %v\n", s, err)
381
382
383
384
385
386
387 }
388
389 func ExampleUnquote() {
390 s, err := strconv.Unquote("You can't unquote a string without quotes")
391 fmt.Printf("%q, %v\n", s, err)
392 s, err = strconv.Unquote("\"The string must be either double-quoted\"")
393 fmt.Printf("%q, %v\n", s, err)
394 s, err = strconv.Unquote("`or backquoted.`")
395 fmt.Printf("%q, %v\n", s, err)
396 s, err = strconv.Unquote("'\u263a'")
397 fmt.Printf("%q, %v\n", s, err)
398 s, err = strconv.Unquote("'\u2639\u2639'")
399 fmt.Printf("%q, %v\n", s, err)
400
401
402
403
404
405
406
407 }
408
409 func ExampleUnquoteChar() {
410 v, mb, t, err := strconv.UnquoteChar(`\"Fran & Freddie's Diner\"`, '"')
411 if err != nil {
412 log.Fatal(err)
413 }
414
415 fmt.Println("value:", string(v))
416 fmt.Println("multibyte:", mb)
417 fmt.Println("tail:", t)
418
419
420
421
422
423 }
424
425 func ExampleNumError() {
426 str := "Not a number"
427 if _, err := strconv.ParseFloat(str, 64); err != nil {
428 e := err.(*strconv.NumError)
429 fmt.Println("Func:", e.Func)
430 fmt.Println("Num:", e.Num)
431 fmt.Println("Err:", e.Err)
432 fmt.Println(err)
433 }
434
435
436
437
438
439
440 }
441
View as plain text