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
139
140 }
141
142 func ExampleFormatInt() {
143 v := int64(-42)
144
145 s10 := strconv.FormatInt(v, 10)
146 fmt.Printf("%T, %v\n", s10, s10)
147
148 s16 := strconv.FormatInt(v, 16)
149 fmt.Printf("%T, %v\n", s16, s16)
150
151
152
153
154 }
155
156 func ExampleFormatUint() {
157 v := uint64(42)
158
159 s10 := strconv.FormatUint(v, 10)
160 fmt.Printf("%T, %v\n", s10, s10)
161
162 s16 := strconv.FormatUint(v, 16)
163 fmt.Printf("%T, %v\n", s16, s16)
164
165
166
167
168 }
169
170 func ExampleIsGraphic() {
171 shamrock := strconv.IsGraphic('☘')
172 fmt.Println(shamrock)
173
174 a := strconv.IsGraphic('a')
175 fmt.Println(a)
176
177 bel := strconv.IsGraphic('\007')
178 fmt.Println(bel)
179
180
181
182
183
184 }
185
186 func ExampleIsPrint() {
187 c := strconv.IsPrint('\u263a')
188 fmt.Println(c)
189
190 bel := strconv.IsPrint('\007')
191 fmt.Println(bel)
192
193
194
195
196 }
197
198 func ExampleItoa() {
199 i := 10
200 s := strconv.Itoa(i)
201 fmt.Printf("%T, %v\n", s, s)
202
203
204
205 }
206
207 func ExampleParseBool() {
208 v := "true"
209 if s, err := strconv.ParseBool(v); err == nil {
210 fmt.Printf("%T, %v\n", s, s)
211 }
212
213
214
215 }
216
217 func ExampleParseFloat() {
218 v := "3.1415926535"
219 if s, err := strconv.ParseFloat(v, 32); err == nil {
220 fmt.Printf("%T, %v\n", s, s)
221 }
222 if s, err := strconv.ParseFloat(v, 64); err == nil {
223 fmt.Printf("%T, %v\n", s, s)
224 }
225 if s, err := strconv.ParseFloat("NaN", 32); err == nil {
226 fmt.Printf("%T, %v\n", s, s)
227 }
228
229 if s, err := strconv.ParseFloat("nan", 32); err == nil {
230 fmt.Printf("%T, %v\n", s, s)
231 }
232 if s, err := strconv.ParseFloat("inf", 32); err == nil {
233 fmt.Printf("%T, %v\n", s, s)
234 }
235 if s, err := strconv.ParseFloat("+Inf", 32); err == nil {
236 fmt.Printf("%T, %v\n", s, s)
237 }
238 if s, err := strconv.ParseFloat("-Inf", 32); err == nil {
239 fmt.Printf("%T, %v\n", s, s)
240 }
241 if s, err := strconv.ParseFloat("-0", 32); err == nil {
242 fmt.Printf("%T, %v\n", s, s)
243 }
244 if s, err := strconv.ParseFloat("+0", 32); err == nil {
245 fmt.Printf("%T, %v\n", s, s)
246 }
247
248
249
250
251
252
253
254
255
256
257
258 }
259
260 func ExampleParseInt() {
261 v32 := "-354634382"
262 if s, err := strconv.ParseInt(v32, 10, 32); err == nil {
263 fmt.Printf("%T, %v\n", s, s)
264 }
265 if s, err := strconv.ParseInt(v32, 16, 32); err == nil {
266 fmt.Printf("%T, %v\n", s, s)
267 }
268
269 v64 := "-3546343826724305832"
270 if s, err := strconv.ParseInt(v64, 10, 64); err == nil {
271 fmt.Printf("%T, %v\n", s, s)
272 }
273 if s, err := strconv.ParseInt(v64, 16, 64); err == nil {
274 fmt.Printf("%T, %v\n", s, s)
275 }
276
277
278
279
280 }
281
282 func ExampleParseUint() {
283 v := "42"
284 if s, err := strconv.ParseUint(v, 10, 32); err == nil {
285 fmt.Printf("%T, %v\n", s, s)
286 }
287 if s, err := strconv.ParseUint(v, 10, 64); err == nil {
288 fmt.Printf("%T, %v\n", s, s)
289 }
290
291
292
293
294 }
295
296 func ExampleQuote() {
297
298 s := strconv.Quote(`"Fran & Freddie's Diner ☺"`)
299 fmt.Println(s)
300
301
302
303 }
304
305 func ExampleQuoteRune() {
306 s := strconv.QuoteRune('☺')
307 fmt.Println(s)
308
309
310
311 }
312
313 func ExampleQuoteRuneToASCII() {
314 s := strconv.QuoteRuneToASCII('☺')
315 fmt.Println(s)
316
317
318
319 }
320
321 func ExampleQuoteRuneToGraphic() {
322 s := strconv.QuoteRuneToGraphic('☺')
323 fmt.Println(s)
324
325 s = strconv.QuoteRuneToGraphic('\u263a')
326 fmt.Println(s)
327
328 s = strconv.QuoteRuneToGraphic('\u000a')
329 fmt.Println(s)
330
331 s = strconv.QuoteRuneToGraphic(' ')
332 fmt.Println(s)
333
334
335
336
337
338
339 }
340
341 func ExampleQuoteToASCII() {
342
343 s := strconv.QuoteToASCII(`"Fran & Freddie's Diner ☺"`)
344 fmt.Println(s)
345
346
347
348 }
349
350 func ExampleQuoteToGraphic() {
351 s := strconv.QuoteToGraphic("☺")
352 fmt.Println(s)
353
354
355 s = strconv.QuoteToGraphic("This is a \u263a \u000a")
356 fmt.Println(s)
357
358 s = strconv.QuoteToGraphic(`" This is a ☺ \n "`)
359 fmt.Println(s)
360
361
362
363
364
365 }
366
367 func ExampleUnquote() {
368 s, err := strconv.Unquote("You can't unquote a string without quotes")
369 fmt.Printf("%q, %v\n", s, err)
370 s, err = strconv.Unquote("\"The string must be either double-quoted\"")
371 fmt.Printf("%q, %v\n", s, err)
372 s, err = strconv.Unquote("`or backquoted.`")
373 fmt.Printf("%q, %v\n", s, err)
374 s, err = strconv.Unquote("'\u263a'")
375 fmt.Printf("%q, %v\n", s, err)
376 s, err = strconv.Unquote("'\u2639\u2639'")
377 fmt.Printf("%q, %v\n", s, err)
378
379
380
381
382
383
384
385 }
386
387 func ExampleUnquoteChar() {
388 v, mb, t, err := strconv.UnquoteChar(`\"Fran & Freddie's Diner\"`, '"')
389 if err != nil {
390 log.Fatal(err)
391 }
392
393 fmt.Println("value:", string(v))
394 fmt.Println("multibyte:", mb)
395 fmt.Println("tail:", t)
396
397
398
399
400
401 }
402
403 func ExampleNumError() {
404 str := "Not a number"
405 if _, err := strconv.ParseFloat(str, 64); err != nil {
406 e := err.(*strconv.NumError)
407 fmt.Println("Func:", e.Func)
408 fmt.Println("Num:", e.Num)
409 fmt.Println("Err:", e.Err)
410 fmt.Println(err)
411 }
412
413
414
415
416
417
418 }
419
View as plain text