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)
137 // fmt.Println uses these arguments to print floats
138 fmt64 := strconv.FormatFloat(v, 'g', -1, 64)
139 fmt.Printf("%T, %v\n", fmt64, fmt64)
142 // string, 3.1415927E+00
143 // string, 3.1415926535E+00
144 // string, 3.1415926535
147 func ExampleFormatInt() {
150 s10 := strconv.FormatInt(v, 10)
151 fmt.Printf("%T, %v\n", s10, s10)
153 s16 := strconv.FormatInt(v, 16)
154 fmt.Printf("%T, %v\n", s16, s16)
161 func ExampleFormatUint() {
164 s10 := strconv.FormatUint(v, 10)
165 fmt.Printf("%T, %v\n", s10, s10)
167 s16 := strconv.FormatUint(v, 16)
168 fmt.Printf("%T, %v\n", s16, s16)
175 func ExampleIsGraphic() {
176 shamrock := strconv.IsGraphic('☘')
177 fmt.Println(shamrock)
179 a := strconv.IsGraphic('a')
182 bel := strconv.IsGraphic('\007')
191 func ExampleIsPrint() {
192 c := strconv.IsPrint('\u263a')
195 bel := strconv.IsPrint('\007')
206 fmt.Printf("%T, %v\n", s, s)
212 func ExampleParseBool() {
214 if s, err := strconv.ParseBool(v); err == nil {
215 fmt.Printf("%T, %v\n", s, s)
222 func ExampleParseFloat() {
224 if s, err := strconv.ParseFloat(v, 32); err == nil {
225 fmt.Printf("%T, %v\n", s, s)
227 if s, err := strconv.ParseFloat(v, 64); err == nil {
228 fmt.Printf("%T, %v\n", s, s)
230 if s, err := strconv.ParseFloat("NaN", 32); err == nil {
231 fmt.Printf("%T, %v\n", s, s)
233 // ParseFloat is case insensitive
234 if s, err := strconv.ParseFloat("nan", 32); err == nil {
235 fmt.Printf("%T, %v\n", s, s)
237 if s, err := strconv.ParseFloat("inf", 32); err == nil {
238 fmt.Printf("%T, %v\n", s, s)
240 if s, err := strconv.ParseFloat("+Inf", 32); err == nil {
241 fmt.Printf("%T, %v\n", s, s)
243 if s, err := strconv.ParseFloat("-Inf", 32); err == nil {
244 fmt.Printf("%T, %v\n", s, s)
246 if s, err := strconv.ParseFloat("-0", 32); err == nil {
247 fmt.Printf("%T, %v\n", s, s)
249 if s, err := strconv.ParseFloat("+0", 32); err == nil {
250 fmt.Printf("%T, %v\n", s, s)
254 // float64, 3.1415927410125732
255 // float64, 3.1415926535
265 func ExampleParseInt() {
267 if s, err := strconv.ParseInt(v32, 10, 32); err == nil {
268 fmt.Printf("%T, %v\n", s, s)
270 if s, err := strconv.ParseInt(v32, 16, 32); err == nil {
271 fmt.Printf("%T, %v\n", s, s)
274 v64 := "-3546343826724305832"
275 if s, err := strconv.ParseInt(v64, 10, 64); err == nil {
276 fmt.Printf("%T, %v\n", s, s)
278 if s, err := strconv.ParseInt(v64, 16, 64); err == nil {
279 fmt.Printf("%T, %v\n", s, s)
284 // int64, -3546343826724305832
287 func ExampleParseUint() {
289 if s, err := strconv.ParseUint(v, 10, 32); err == nil {
290 fmt.Printf("%T, %v\n", s, s)
292 if s, err := strconv.ParseUint(v, 10, 64); err == nil {
293 fmt.Printf("%T, %v\n", s, s)
301 func ExampleQuote() {
302 // This string literal contains a tab character.
303 s := strconv.Quote(`"Fran & Freddie's Diner ☺"`)
307 // "\"Fran & Freddie's Diner\t☺\""
310 func ExampleQuoteRune() {
311 s := strconv.QuoteRune('☺')
318 func ExampleQuoteRuneToASCII() {
319 s := strconv.QuoteRuneToASCII('☺')
326 func ExampleQuoteRuneToGraphic() {
327 s := strconv.QuoteRuneToGraphic('☺')
330 s = strconv.QuoteRuneToGraphic('\u263a')
333 s = strconv.QuoteRuneToGraphic('\u000a')
336 s = strconv.QuoteRuneToGraphic(' ') // tab character
346 func ExampleQuoteToASCII() {
347 // This string literal contains a tab character.
348 s := strconv.QuoteToASCII(`"Fran & Freddie's Diner ☺"`)
352 // "\"Fran & Freddie's Diner\t\u263a\""
355 func ExampleQuoteToGraphic() {
356 s := strconv.QuoteToGraphic("☺")
359 // This string literal contains a tab character.
360 s = strconv.QuoteToGraphic("This is a \u263a \u000a")
363 s = strconv.QuoteToGraphic(`" This is a ☺ \n "`)
369 // "\" This is a ☺ \\n \""
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)
383 // "", invalid syntax
384 // "\"double-quoted string\"", <nil>
385 // "`or backquoted`", <nil>
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'") // single character only allowed in single quotes
397 fmt.Printf("%q, %v\n", s, err)
398 s, err = strconv.Unquote("'\u2639\u2639'")
399 fmt.Printf("%q, %v\n", s, err)
402 // "", invalid syntax
403 // "The string must be either double-quoted", <nil>
404 // "or backquoted.", <nil>
406 // "", invalid syntax
409 func ExampleUnquoteChar() {
410 v, mb, t, err := strconv.UnquoteChar(`\"Fran & Freddie's Diner\"`, '"')
415 fmt.Println("value:", string(v))
416 fmt.Println("multibyte:", mb)
417 fmt.Println("tail:", t)
422 // tail: Fran & Freddie's Diner\"
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)
438 // Err: invalid syntax
439 // strconv.ParseFloat: parsing "Not a number": invalid syntax