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. 4 5package strconv_test 6 7import ( 8 "fmt" 9 "log" 10 "strconv" 11) 12 13func ExampleAppendBool() { 14 b := []byte("bool:") 15 b = strconv.AppendBool(b, true) 16 fmt.Println(string(b)) 17 18 // Output: 19 // bool:true 20} 21 22func 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 // Output: 32 // float32:3.1415927E+00 33 // float64:3.1415926535E+00 34} 35 36func 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 // Output: 46 // int (base 10):-42 47 // int (base 16):-2a 48} 49 50func ExampleAppendQuote() { 51 b := []byte("quote:") 52 b = strconv.AppendQuote(b, `"Fran & Freddie's Diner"`) 53 fmt.Println(string(b)) 54 55 // Output: 56 // quote:"\"Fran & Freddie's Diner\"" 57} 58 59func ExampleAppendQuoteRune() { 60 b := []byte("rune:") 61 b = strconv.AppendQuoteRune(b, '☺') 62 fmt.Println(string(b)) 63 64 // Output: 65 // rune:'☺' 66} 67 68func ExampleAppendQuoteRuneToASCII() { 69 b := []byte("rune (ascii):") 70 b = strconv.AppendQuoteRuneToASCII(b, '☺') 71 fmt.Println(string(b)) 72 73 // Output: 74 // rune (ascii):'\u263a' 75} 76 77func ExampleAppendQuoteToASCII() { 78 b := []byte("quote (ascii):") 79 b = strconv.AppendQuoteToASCII(b, `"Fran & Freddie's Diner"`) 80 fmt.Println(string(b)) 81 82 // Output: 83 // quote (ascii):"\"Fran & Freddie's Diner\"" 84} 85 86func 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 // Output: 96 // uint (base 10):42 97 // uint (base 16):2a 98} 99 100func ExampleAtoi() { 101 v := "10" 102 if s, err := strconv.Atoi(v); err == nil { 103 fmt.Printf("%T, %v", s, s) 104 } 105 106 // Output: 107 // int, 10 108} 109 110func ExampleCanBackquote() { 111 fmt.Println(strconv.CanBackquote("Fran & Freddie's Diner ☺")) 112 fmt.Println(strconv.CanBackquote("`can't backquote this`")) 113 114 // Output: 115 // true 116 // false 117} 118 119func ExampleFormatBool() { 120 v := true 121 s := strconv.FormatBool(v) 122 fmt.Printf("%T, %v\n", s, s) 123 124 // Output: 125 // string, true 126} 127 128func 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 // fmt.Println uses these arguments to print floats 138 fmt64 := strconv.FormatFloat(v, 'g', -1, 64) 139 fmt.Printf("%T, %v\n", fmt64, fmt64) 140 141 // Output: 142 // string, 3.1415927E+00 143 // string, 3.1415926535E+00 144 // string, 3.1415926535 145} 146 147func 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 // Output: 157 // string, -42 158 // string, -2a 159} 160 161func 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 // Output: 171 // string, 42 172 // string, 2a 173} 174 175func 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 // Output: 186 // true 187 // true 188 // false 189} 190 191func ExampleIsPrint() { 192 c := strconv.IsPrint('\u263a') 193 fmt.Println(c) 194 195 bel := strconv.IsPrint('\007') 196 fmt.Println(bel) 197 198 // Output: 199 // true 200 // false 201} 202 203func ExampleItoa() { 204 i := 10 205 s := strconv.Itoa(i) 206 fmt.Printf("%T, %v\n", s, s) 207 208 // Output: 209 // string, 10 210} 211 212func ExampleParseBool() { 213 v := "true" 214 if s, err := strconv.ParseBool(v); err == nil { 215 fmt.Printf("%T, %v\n", s, s) 216 } 217 218 // Output: 219 // bool, true 220} 221 222func 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 // ParseFloat is case insensitive 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 // Output: 254 // float64, 3.1415927410125732 255 // float64, 3.1415926535 256 // float64, NaN 257 // float64, NaN 258 // float64, +Inf 259 // float64, +Inf 260 // float64, -Inf 261 // float64, -0 262 // float64, 0 263} 264 265func 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 // Output: 283 // int64, -354634382 284 // int64, -3546343826724305832 285} 286 287func 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 // Output: 297 // uint64, 42 298 // uint64, 42 299} 300 301func ExampleQuote() { 302 // This string literal contains a tab character. 303 s := strconv.Quote(`"Fran & Freddie's Diner ☺"`) 304 fmt.Println(s) 305 306 // Output: 307 // "\"Fran & Freddie's Diner\t☺\"" 308} 309 310func ExampleQuoteRune() { 311 s := strconv.QuoteRune('☺') 312 fmt.Println(s) 313 314 // Output: 315 // '☺' 316} 317 318func ExampleQuoteRuneToASCII() { 319 s := strconv.QuoteRuneToASCII('☺') 320 fmt.Println(s) 321 322 // Output: 323 // '\u263a' 324} 325 326func 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(' ') // tab character 337 fmt.Println(s) 338 339 // Output: 340 // '☺' 341 // '☺' 342 // '\n' 343 // '\t' 344} 345 346func ExampleQuoteToASCII() { 347 // This string literal contains a tab character. 348 s := strconv.QuoteToASCII(`"Fran & Freddie's Diner ☺"`) 349 fmt.Println(s) 350 351 // Output: 352 // "\"Fran & Freddie's Diner\t\u263a\"" 353} 354 355func ExampleQuoteToGraphic() { 356 s := strconv.QuoteToGraphic("☺") 357 fmt.Println(s) 358 359 // This string literal contains a tab character. 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 // Output: 367 // "☺" 368 // "This is a ☺\t\n" 369 // "\" This is a ☺ \\n \"" 370} 371 372func 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 // Output: 383 // "", invalid syntax 384 // "\"double-quoted string\"", <nil> 385 // "`or backquoted`", <nil> 386 // "'☺'", <nil> 387} 388 389func 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) 400 401 // Output: 402 // "", invalid syntax 403 // "The string must be either double-quoted", <nil> 404 // "or backquoted.", <nil> 405 // "☺", <nil> 406 // "", invalid syntax 407} 408 409func 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 // Output: 420 // value: " 421 // multibyte: false 422 // tail: Fran & Freddie's Diner\" 423} 424 425func 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 // Output: 436 // Func: ParseFloat 437 // Num: Not a number 438 // Err: invalid syntax 439 // strconv.ParseFloat: parsing "Not a number": invalid syntax 440} 441