1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 func ExampleAppendBool() {
15 b
= strconv
.AppendBool(b
, true)
16 fmt
.Println(string(b
))
22 func ExampleAppendFloat() {
23 b32
:= []byte("float32:")
24 b32
= strconv
.AppendFloat(b32
, 3.1415926535, 'E', -1, 32)
25 fmt
.Println(string(b32
))
27 b64
:= []byte("float64:")
28 b64
= strconv
.AppendFloat(b64
, 3.1415926535, 'E', -1, 64)
29 fmt
.Println(string(b64
))
32 // float32:3.1415927E+00
33 // float64:3.1415926535E+00
36 func ExampleAppendInt() {
37 b10
:= []byte("int (base 10):")
38 b10
= strconv
.AppendInt(b10
, -42, 10)
39 fmt
.Println(string(b10
))
41 b16
:= []byte("int (base 16):")
42 b16
= strconv
.AppendInt(b16
, -42, 16)
43 fmt
.Println(string(b16
))
50 func ExampleAppendQuote() {
52 b
= strconv
.AppendQuote(b
, `"Fran & Freddie's Diner"`)
53 fmt
.Println(string(b
))
56 // quote:"\"Fran & Freddie's Diner\""
59 func ExampleAppendQuoteRune() {
61 b
= strconv
.AppendQuoteRune(b
, '☺')
62 fmt
.Println(string(b
))
68 func ExampleAppendQuoteRuneToASCII() {
69 b
:= []byte("rune (ascii):")
70 b
= strconv
.AppendQuoteRuneToASCII(b
, '☺')
71 fmt
.Println(string(b
))
74 // rune (ascii):'\u263a'
77 func ExampleAppendQuoteToASCII() {
78 b
:= []byte("quote (ascii):")
79 b
= strconv
.AppendQuoteToASCII(b
, `"Fran & Freddie's Diner"`)
80 fmt
.Println(string(b
))
83 // quote (ascii):"\"Fran & Freddie's Diner\""
86 func ExampleAppendUint() {
87 b10
:= []byte("uint (base 10):")
88 b10
= strconv
.AppendUint(b10
, 42, 10)
89 fmt
.Println(string(b10
))
91 b16
:= []byte("uint (base 16):")
92 b16
= strconv
.AppendUint(b16
, 42, 16)
93 fmt
.Println(string(b16
))
102 if s
, err
:= strconv
.Atoi(v
); err
== nil {
103 fmt
.Printf("%T, %v", s
, s
)
110 func ExampleCanBackquote() {
111 fmt
.Println(strconv
.CanBackquote("Fran & Freddie's Diner ☺"))
112 fmt
.Println(strconv
.CanBackquote("`can't backquote this`"))
119 func ExampleFormatBool() {
121 s
:= strconv
.FormatBool(v
)
122 fmt
.Printf("%T, %v\n", s
, s
)
128 func ExampleFormatFloat() {
131 s32
:= strconv
.FormatFloat(v
, 'E', -1, 32)
132 fmt
.Printf("%T, %v\n", s32
, s32
)
134 s64
:= strconv
.FormatFloat(v
, 'E', -1, 64)
135 fmt
.Printf("%T, %v\n", s64
, s64
)
138 // string, 3.1415927E+00
139 // string, 3.1415926535E+00
142 func ExampleFormatInt() {
145 s10
:= strconv
.FormatInt(v
, 10)
146 fmt
.Printf("%T, %v\n", s10
, s10
)
148 s16
:= strconv
.FormatInt(v
, 16)
149 fmt
.Printf("%T, %v\n", s16
, s16
)
156 func ExampleFormatUint() {
159 s10
:= strconv
.FormatUint(v
, 10)
160 fmt
.Printf("%T, %v\n", s10
, s10
)
162 s16
:= strconv
.FormatUint(v
, 16)
163 fmt
.Printf("%T, %v\n", s16
, s16
)
170 func ExampleIsGraphic() {
171 shamrock
:= strconv
.IsGraphic('☘')
172 fmt
.Println(shamrock
)
174 a
:= strconv
.IsGraphic('a')
177 bel
:= strconv
.IsGraphic('\007')
186 func ExampleIsPrint() {
187 c
:= strconv
.IsPrint('\u263a')
190 bel
:= strconv
.IsPrint('\007')
201 fmt
.Printf("%T, %v\n", s
, s
)
207 func ExampleParseBool() {
209 if s
, err
:= strconv
.ParseBool(v
); err
== nil {
210 fmt
.Printf("%T, %v\n", s
, s
)
217 func ExampleParseFloat() {
219 if s
, err
:= strconv
.ParseFloat(v
, 32); err
== nil {
220 fmt
.Printf("%T, %v\n", s
, s
)
222 if s
, err
:= strconv
.ParseFloat(v
, 64); err
== nil {
223 fmt
.Printf("%T, %v\n", s
, s
)
225 if s
, err
:= strconv
.ParseFloat("NaN", 32); err
== nil {
226 fmt
.Printf("%T, %v\n", s
, s
)
228 // ParseFloat is case insensitive
229 if s
, err
:= strconv
.ParseFloat("nan", 32); err
== nil {
230 fmt
.Printf("%T, %v\n", s
, s
)
232 if s
, err
:= strconv
.ParseFloat("inf", 32); err
== nil {
233 fmt
.Printf("%T, %v\n", s
, s
)
235 if s
, err
:= strconv
.ParseFloat("+Inf", 32); err
== nil {
236 fmt
.Printf("%T, %v\n", s
, s
)
238 if s
, err
:= strconv
.ParseFloat("-Inf", 32); err
== nil {
239 fmt
.Printf("%T, %v\n", s
, s
)
241 if s
, err
:= strconv
.ParseFloat("-0", 32); err
== nil {
242 fmt
.Printf("%T, %v\n", s
, s
)
244 if s
, err
:= strconv
.ParseFloat("+0", 32); err
== nil {
245 fmt
.Printf("%T, %v\n", s
, s
)
249 // float64, 3.1415927410125732
250 // float64, 3.1415926535
260 func ExampleParseInt() {
262 if s
, err
:= strconv
.ParseInt(v32
, 10, 32); err
== nil {
263 fmt
.Printf("%T, %v\n", s
, s
)
265 if s
, err
:= strconv
.ParseInt(v32
, 16, 32); err
== nil {
266 fmt
.Printf("%T, %v\n", s
, s
)
269 v64
:= "-3546343826724305832"
270 if s
, err
:= strconv
.ParseInt(v64
, 10, 64); err
== nil {
271 fmt
.Printf("%T, %v\n", s
, s
)
273 if s
, err
:= strconv
.ParseInt(v64
, 16, 64); err
== nil {
274 fmt
.Printf("%T, %v\n", s
, s
)
279 // int64, -3546343826724305832
282 func ExampleParseUint() {
284 if s
, err
:= strconv
.ParseUint(v
, 10, 32); err
== nil {
285 fmt
.Printf("%T, %v\n", s
, s
)
287 if s
, err
:= strconv
.ParseUint(v
, 10, 64); err
== nil {
288 fmt
.Printf("%T, %v\n", s
, s
)
296 func ExampleQuote() {
297 // This string literal contains a tab character.
298 s
:= strconv
.Quote(`"Fran & Freddie's Diner ☺"`)
302 // "\"Fran & Freddie's Diner\t☺\""
305 func ExampleQuoteRune() {
306 s
:= strconv
.QuoteRune('☺')
313 func ExampleQuoteRuneToASCII() {
314 s
:= strconv
.QuoteRuneToASCII('☺')
321 func ExampleQuoteRuneToGraphic() {
322 s
:= strconv
.QuoteRuneToGraphic('☺')
325 s
= strconv
.QuoteRuneToGraphic('\u263a')
328 s
= strconv
.QuoteRuneToGraphic('\u000a')
331 s
= strconv
.QuoteRuneToGraphic(' ') // tab character
341 func ExampleQuoteToASCII() {
342 // This string literal contains a tab character.
343 s
:= strconv
.QuoteToASCII(`"Fran & Freddie's Diner ☺"`)
347 // "\"Fran & Freddie's Diner\t\u263a\""
350 func ExampleQuoteToGraphic() {
351 s
:= strconv
.QuoteToGraphic("☺")
354 // This string literal contains a tab character.
355 s
= strconv
.QuoteToGraphic("This is a \u263a \u000a")
358 s
= strconv
.QuoteToGraphic(`" This is a ☺ \n "`)
364 // "\" This is a ☺ \\n \""
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'") // single character only allowed in single quotes
375 fmt
.Printf("%q, %v\n", s
, err
)
376 s
, err
= strconv
.Unquote("'\u2639\u2639'")
377 fmt
.Printf("%q, %v\n", s
, err
)
380 // "", invalid syntax
381 // "The string must be either double-quoted", <nil>
382 // "or backquoted.", <nil>
384 // "", invalid syntax
387 func ExampleUnquoteChar() {
388 v
, mb
, t
, err
:= strconv
.UnquoteChar(`\"Fran & Freddie's Diner\"`, '"')
393 fmt
.Println("value:", string(v
))
394 fmt
.Println("multibyte:", mb
)
395 fmt
.Println("tail:", t
)
400 // tail: Fran & Freddie's Diner\"
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
)
416 // Err: invalid syntax
417 // strconv.ParseFloat: parsing "Not a number": invalid syntax