github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/strconv/example_test.go (about) 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 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 // Output: 19 // bool:true 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 // Output: 32 // float32:3.1415927E+00 33 // float64:3.1415926535E+00 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 // Output: 46 // int (base 10):-42 47 // int (base 16):-2a 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 // Output: 56 // quote:"\"Fran & Freddie's Diner\"" 57 } 58 59 func ExampleAppendQuoteRune() { 60 b := []byte("rune:") 61 b = strconv.AppendQuoteRune(b, '☺') 62 fmt.Println(string(b)) 63 64 // Output: 65 // rune:'☺' 66 } 67 68 func 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 77 func 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 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 // Output: 96 // uint (base 10):42 97 // uint (base 16):2a 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 // Output: 107 // int, 10 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 // Output: 115 // true 116 // false 117 } 118 119 func 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 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 // Output: 138 // string, 3.1415927E+00 139 // string, 3.1415926535E+00 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 // Output: 152 // string, -42 153 // string, -2a 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 // Output: 166 // string, 42 167 // string, 2a 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 // Output: 181 // true 182 // true 183 // false 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 // Output: 194 // true 195 // false 196 } 197 198 func ExampleItoa() { 199 i := 10 200 s := strconv.Itoa(i) 201 fmt.Printf("%T, %v\n", s, s) 202 203 // Output: 204 // string, 10 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 // Output: 214 // bool, true 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 226 // Output: 227 // float64, 3.1415927410125732 228 // float64, 3.1415926535 229 } 230 231 func ExampleParseInt() { 232 v32 := "-354634382" 233 if s, err := strconv.ParseInt(v32, 10, 32); err == nil { 234 fmt.Printf("%T, %v\n", s, s) 235 } 236 if s, err := strconv.ParseInt(v32, 16, 32); err == nil { 237 fmt.Printf("%T, %v\n", s, s) 238 } 239 240 v64 := "-3546343826724305832" 241 if s, err := strconv.ParseInt(v64, 10, 64); err == nil { 242 fmt.Printf("%T, %v\n", s, s) 243 } 244 if s, err := strconv.ParseInt(v64, 16, 64); err == nil { 245 fmt.Printf("%T, %v\n", s, s) 246 } 247 248 // Output: 249 // int64, -354634382 250 // int64, -3546343826724305832 251 } 252 253 func ExampleParseUint() { 254 v := "42" 255 if s, err := strconv.ParseUint(v, 10, 32); err == nil { 256 fmt.Printf("%T, %v\n", s, s) 257 } 258 if s, err := strconv.ParseUint(v, 10, 64); err == nil { 259 fmt.Printf("%T, %v\n", s, s) 260 } 261 262 // Output: 263 // uint64, 42 264 // uint64, 42 265 } 266 267 func ExampleQuote() { 268 s := strconv.Quote(`"Fran & Freddie's Diner ☺"`) // there is a tab character inside the string literal 269 fmt.Println(s) 270 271 // Output: 272 // "\"Fran & Freddie's Diner\t☺\"" 273 } 274 275 func ExampleQuoteRune() { 276 s := strconv.QuoteRune('☺') 277 fmt.Println(s) 278 279 // Output: 280 // '☺' 281 } 282 283 func ExampleQuoteRuneToASCII() { 284 s := strconv.QuoteRuneToASCII('☺') 285 fmt.Println(s) 286 287 // Output: 288 // '\u263a' 289 } 290 291 func ExampleQuoteRuneToGraphic() { 292 s := strconv.QuoteRuneToGraphic('☺') 293 fmt.Println(s) 294 295 s = strconv.QuoteRuneToGraphic('\u263a') 296 fmt.Println(s) 297 298 s = strconv.QuoteRuneToGraphic('\u000a') 299 fmt.Println(s) 300 301 s = strconv.QuoteRuneToGraphic(' ') // tab character 302 fmt.Println(s) 303 304 // Output: 305 // '☺' 306 // '☺' 307 // '\n' 308 // '\t' 309 } 310 311 func ExampleQuoteToASCII() { 312 s := strconv.QuoteToASCII(`"Fran & Freddie's Diner ☺"`) // there is a tab character inside the string literal 313 fmt.Println(s) 314 315 // Output: 316 // "\"Fran & Freddie's Diner\t\u263a\"" 317 } 318 319 func ExampleQuoteToGraphic() { 320 s := strconv.QuoteToGraphic("☺") 321 fmt.Println(s) 322 323 s = strconv.QuoteToGraphic("This is a \u263a \u000a") // there is a tab character inside the string literal 324 fmt.Println(s) 325 326 s = strconv.QuoteToGraphic(`" This is a ☺ \n "`) 327 fmt.Println(s) 328 329 // Output: 330 // "☺" 331 // "This is a ☺\t\n" 332 // "\" This is a ☺ \\n \"" 333 } 334 335 func ExampleUnquote() { 336 s, err := strconv.Unquote("You can't unquote a string without quotes") 337 fmt.Printf("%q, %v\n", s, err) 338 s, err = strconv.Unquote("\"The string must be either double-quoted\"") 339 fmt.Printf("%q, %v\n", s, err) 340 s, err = strconv.Unquote("`or backquoted.`") 341 fmt.Printf("%q, %v\n", s, err) 342 s, err = strconv.Unquote("'\u263a'") // single character only allowed in single quotes 343 fmt.Printf("%q, %v\n", s, err) 344 s, err = strconv.Unquote("'\u2639\u2639'") 345 fmt.Printf("%q, %v\n", s, err) 346 347 // Output: 348 // "", invalid syntax 349 // "The string must be either double-quoted", <nil> 350 // "or backquoted.", <nil> 351 // "☺", <nil> 352 // "", invalid syntax 353 } 354 355 func ExampleUnquoteChar() { 356 v, mb, t, err := strconv.UnquoteChar(`\"Fran & Freddie's Diner\"`, '"') 357 if err != nil { 358 log.Fatal(err) 359 } 360 361 fmt.Println("value:", string(v)) 362 fmt.Println("multibyte:", mb) 363 fmt.Println("tail:", t) 364 365 // Output: 366 // value: " 367 // multibyte: false 368 // tail: Fran & Freddie's Diner\" 369 } 370 371 func ExampleNumError() { 372 str := "Not a number" 373 if _, err := strconv.ParseFloat(str, 64); err != nil { 374 e := err.(*strconv.NumError) 375 fmt.Println("Func:", e.Func) 376 fmt.Println("Num:", e.Num) 377 fmt.Println("Err:", e.Err) 378 fmt.Println(err) 379 } 380 381 // Output: 382 // Func: ParseFloat 383 // Num: Not a number 384 // Err: invalid syntax 385 // strconv.ParseFloat: parsing "Not a number": invalid syntax 386 }