Chinaunix首页 | 论坛 | 博客
  • 博客访问: 334861
  • 博文数量: 214
  • 博客积分: 4258
  • 博客等级: 上校
  • 技术积分: 2021
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-02 09:16
个人简介

http://blog.csdn.net/ly21st http://ly21st.blog.chinaunix.net

文章分类

全部博文(214)

文章存档

2018年(16)

2015年(1)

2014年(2)

2012年(22)

2011年(173)

分类: C/C++

2018-01-30 14:35:58


点击(此处)折叠或打开

  1. // strconv_my project main.go
  2. package main

  3. // Copyright 2015 The Go Authors. All rights reserved.
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file.

  6. import (
  7.     "fmt"
  8.     "log"
  9.     "strconv"
  10. )

  11. func ExampleAppendBool() {
  12.     b := []byte("bool:")
  13.     b = strconv.AppendBool(b, true)
  14.     fmt.Println(string(b))

  15.     // Output:
  16.     // bool:true
  17. }

  18. func ExampleAppendFloat() {
  19.     b32 := []byte("float32:")
  20.     b32 = strconv.AppendFloat(b32, 3.1415926535, 'E', -1, 32)
  21.     fmt.Println(string(b32))

  22.     b64 := []byte("float64:")
  23.     b64 = strconv.AppendFloat(b64, 3.1415926535, 'E', -1, 64)
  24.     fmt.Println(string(b64))

  25.     // Output:
  26.     // float32:3.1415927E+00
  27.     // float64:3.1415926535E+00
  28. }

  29. func ExampleAppendInt() {
  30.     b10 := []byte("int (base 10):")
  31.     b10 = strconv.AppendInt(b10, -42, 10)
  32.     fmt.Println(string(b10))

  33.     b16 := []byte("int (base 16):")
  34.     b16 = strconv.AppendInt(b16, -42, 16)
  35.     fmt.Println(string(b16))

  36.     // Output:
  37.     // int (base 10):-42
  38.     // int (base 16):-2a
  39. }

  40. func ExampleAppendQuote() {
  41.     b := []byte("quote:")
  42.     b = strconv.AppendQuote(b, `"Fran & Freddie's Diner"`)
  43.     fmt.Println(string(b))

  44.     // Output:
  45.     // quote:"\"Fran & Freddie's Diner\""
  46. }

  47. func ExampleAppendQuoteRune() {
  48.     b := []byte("rune:")
  49.     b = strconv.AppendQuoteRune(b, '?')
  50.     fmt.Println(string(b))

  51.     // Output:
  52.     // rune:'?'
  53. }

  54. func ExampleAppendQuoteRuneToASCII() {
  55.     b := []byte("rune (ascii):")
  56.     b = strconv.AppendQuoteRuneToASCII(b, '?')
  57.     fmt.Println(string(b))

  58.     // Output:
  59.     // rune (ascii):'\u263a'
  60. }

  61. func ExampleAppendQuoteToASCII() {
  62.     b := []byte("quote (ascii):")
  63.     b = strconv.AppendQuoteToASCII(b, `"Fran & Freddie's Diner"`)
  64.     fmt.Println(string(b))

  65.     // Output:
  66.     // quote (ascii):"\"Fran & Freddie's Diner\""
  67. }

  68. func ExampleAppendUint() {
  69.     b10 := []byte("uint (base 10):")
  70.     b10 = strconv.AppendUint(b10, 42, 10)
  71.     fmt.Println(string(b10))

  72.     b16 := []byte("uint (base 16):")
  73.     b16 = strconv.AppendUint(b16, 42, 16)
  74.     fmt.Println(string(b16))

  75.     // Output:
  76.     // uint (base 10):42
  77.     // uint (base 16):2a
  78. }

  79. func ExampleAtoi() {
  80.     v := "10"
  81.     if s, err := strconv.Atoi(v); err == nil {
  82.         fmt.Printf("%T, %v", s, s)
  83.     }

  84.     // Output:
  85.     // int, 10
  86. }

  87. func ExampleCanBackquote() {
  88.     fmt.Println(strconv.CanBackquote("Fran & Freddie's Diner ?"))
  89.     fmt.Println(strconv.CanBackquote("`can't backquote this`"))

  90.     // Output:
  91.     // true
  92.     // false
  93. }

  94. func ExampleFormatBool() {
  95.     v := true
  96.     s := strconv.FormatBool(v)
  97.     fmt.Printf("%T, %v\n", s, s)

  98.     // Output:
  99.     // string, true
  100. }

  101. func ExampleFormatFloat() {
  102.     v := 3.1415926535

  103.     s32 := strconv.FormatFloat(v, 'E', -1, 32)
  104.     fmt.Printf("%T, %v\n", s32, s32)

  105.     s64 := strconv.FormatFloat(v, 'E', -1, 64)
  106.     fmt.Printf("%T, %v\n", s64, s64)

  107.     // Output:
  108.     // string, 3.1415927E+00
  109.     // string, 3.1415926535E+00
  110. }

  111. func ExampleFormatInt() {
  112.     v := int64(-42)

  113.     s10 := strconv.FormatInt(v, 10)
  114.     fmt.Printf("%T, %v\n", s10, s10)

  115.     s16 := strconv.FormatInt(v, 16)
  116.     fmt.Printf("%T, %v\n", s16, s16)

  117.     // Output:
  118.     // string, -42
  119.     // string, -2a
  120. }

  121. func ExampleFormatUint() {
  122.     v := uint64(42)

  123.     s10 := strconv.FormatUint(v, 10)
  124.     fmt.Printf("%T, %v\n", s10, s10)

  125.     s16 := strconv.FormatUint(v, 16)
  126.     fmt.Printf("%T, %v\n", s16, s16)

  127.     // Output:
  128.     // string, 42
  129.     // string, 2a
  130. }

  131. func ExampleIsPrint() {
  132.     c := strconv.IsPrint('\u263a')
  133.     fmt.Println(c)

  134.     bel := strconv.IsPrint('\007')
  135.     fmt.Println(bel)

  136.     // Output:
  137.     // true
  138.     // false
  139. }

  140. func ExampleItoa() {
  141.     i := 10
  142.     s := strconv.Itoa(i)
  143.     fmt.Printf("%T, %v\n", s, s)

  144.     // Output:
  145.     // string, 10
  146. }

  147. func ExampleParseBool() {
  148.     v := "true"
  149.     if s, err := strconv.ParseBool(v); err == nil {
  150.         fmt.Printf("%T, %v\n", s, s)
  151.     }

  152.     // Output:
  153.     // bool, true
  154. }

  155. func ExampleParseFloat() {
  156.     v := "3.1415926535"
  157.     if s, err := strconv.ParseFloat(v, 32); err == nil {
  158.         fmt.Printf("%T, %v\n", s, s)
  159.     }
  160.     if s, err := strconv.ParseFloat(v, 64); err == nil {
  161.         fmt.Printf("%T, %v\n", s, s)
  162.     }

  163.     // Output:
  164.     // float64, 3.1415927410125732
  165.     // float64, 3.1415926535
  166. }

  167. func ExampleParseInt() {
  168.     v32 := "-354634382"
  169.     if s, err := strconv.ParseInt(v32, 10, 32); err == nil {
  170.         fmt.Printf("%T, %v\n", s, s)
  171.     }
  172.     if s, err := strconv.ParseInt(v32, 16, 32); err == nil {
  173.         fmt.Printf("%T, %v\n", s, s)
  174.     }

  175.     v64 := "-3546343826724305832"
  176.     if s, err := strconv.ParseInt(v64, 10, 64); err == nil {
  177.         fmt.Printf("%T, %v\n", s, s)
  178.     }
  179.     if s, err := strconv.ParseInt(v64, 16, 64); err == nil {
  180.         fmt.Printf("%T, %v\n", s, s)
  181.     }

  182.     // Output:
  183.     // int64, -354634382
  184.     // int64, -3546343826724305832
  185. }

  186. func ExampleParseUint() {
  187.     v := "42"
  188.     if s, err := strconv.ParseUint(v, 10, 32); err == nil {
  189.         fmt.Printf("%T, %v\n", s, s)
  190.     }
  191.     if s, err := strconv.ParseUint(v, 10, 64); err == nil {
  192.         fmt.Printf("%T, %v\n", s, s)
  193.     }

  194.     // Output:
  195.     // uint64, 42
  196.     // uint64, 42
  197. }

  198. func ExampleQuote() {
  199.     s := strconv.Quote(`"Fran & Freddie's Diner    ?"`)
  200.     fmt.Println(s)

  201.     // Output:
  202.     // "\"Fran & Freddie's Diner\t?\""
  203. }

  204. func ExampleQuoteRune() {
  205.     s := strconv.QuoteRune('?')
  206.     fmt.Println(s)

  207.     // Output:
  208.     // '?'
  209. }

  210. func ExampleQuoteRuneToASCII() {
  211.     s := strconv.QuoteRuneToASCII('?')
  212.     fmt.Println(s)

  213.     // Output:
  214.     // '\u263a'
  215. }

  216. func ExampleQuoteToASCII() {
  217.     s := strconv.QuoteToASCII(`"Fran & Freddie's Diner    ?"`)
  218.     fmt.Println(s)

  219.     // Output:
  220.     // "\"Fran & Freddie's Diner\t\u263a\""
  221. }

  222. func ExampleUnquote() {
  223.     test := func(s string) {
  224.         t, err := strconv.Unquote(s)
  225.         if err != nil {
  226.             fmt.Printf("Unquote(%#v): %v\n", s, err)
  227.         } else {
  228.             fmt.Printf("Unquote(%#v) = %v\n", s, t)
  229.         }
  230.     }

  231.     s := `\"Fran & Freddie's Diner\t\u263a\"\"`
  232.     // If the string doesn't have quotes, it can't be unquoted.
  233.     test(s) // invalid syntax
  234.     test("`" + s + "`")
  235.     test(`"` + s + `"`)
  236.     test(`'\u263a'`)

  237.     // Output:
  238.     // Unquote("\\\"Fran & Freddie's Diner\\t\\u263a\\\"\\\""): invalid syntax
  239.     // Unquote("`\\\"Fran & Freddie's Diner\\t\\u263a\\\"\\\"`") = \"Fran & Freddie's Diner\t\u263a\"\"
  240.     // Unquote("\"\\\"Fran & Freddie's Diner\\t\\u263a\\\"\\\"\"") = "Fran & Freddie's Diner    ?""
  241.     // Unquote("'\\u263a'") = ?
  242. }

  243. func ExampleUnquoteChar() {
  244.     v, mb, t, err := strconv.UnquoteChar(`\"Fran & Freddie's Diner\"`, '"')
  245.     if err != nil {
  246.         log.Fatal(err)
  247.     }

  248.     fmt.Println("value:", string(v))
  249.     fmt.Println("multibyte:", mb)
  250.     fmt.Println("tail:", t)

  251.     // Output:
  252.     // value: "
  253.     // multibyte: false
  254.     // tail: Fran & Freddie's Diner\"
  255. }

  256. func ExampleNumError() {
  257.     str := "Not a number"
  258.     if _, err := strconv.ParseFloat(str, 64); err != nil {
  259.         e := err.(*strconv.NumError)
  260.         fmt.Println("Func:", e.Func)
  261.         fmt.Println("Num:", e.Num)
  262.         fmt.Println("Err:", e.Err)
  263.         fmt.Println(err)
  264.     }

  265.     // Output:
  266.     // Func: ParseFloat
  267.     // Num: Not a number
  268.     // Err: invalid syntax
  269.     // strconv.ParseFloat: parsing "Not a number": invalid syntax
  270. }

  271. func main() {
  272.     fmt.Println("---begin test ExampleAppendBool---")
  273.     ExampleAppendBool()
  274.     fmt.Println("### end test ExampleAppendBool ###\n")

  275.     fmt.Println("---begin test ExampleAppendFloat---")
  276.     ExampleAppendFloat()
  277.     fmt.Println("### end test ExampleAppendFloat ###\n")

  278.     fmt.Println("---begin test ExampleAppendInt---")
  279.     ExampleAppendInt()
  280.     fmt.Println("### end test ExampleAppendInt ###\n")

  281.     fmt.Println("---begin test ExampleAppendQuote---")
  282.     ExampleAppendQuote()
  283.     fmt.Println("### end test ExampleAppendQuote ###\n")

  284.     fmt.Println("---begin test ExampleAppendQuoteRune---")
  285.     ExampleAppendQuoteRune()
  286.     fmt.Println("### end test ExampleAppendQuoteRune ###\n")

  287.     fmt.Println("---begin test ExampleAppendQuoteRuneToASCII---")
  288.     ExampleAppendQuoteRuneToASCII()
  289.     fmt.Println("### end test ExampleAppendQuoteRuneToASCII ###\n")

  290.     fmt.Println("---begin test ExampleAppendQuoteToASCII---")
  291.     ExampleAppendQuoteToASCII()
  292.     fmt.Println("### end test ExampleAppendQuoteToASCII ###\n")

  293.     fmt.Println("---begin test ExampleAppendUint---")
  294.     ExampleAppendUint()
  295.     fmt.Println("### end test ExampleAppendUint ###\n")

  296.     fmt.Println("---begin test ExampleAtoi---")
  297.     ExampleAtoi()
  298.     fmt.Println("### end test ExampleAtoi ###\n")

  299.     fmt.Println("---begin test ExampleCanBackquote---")
  300.     ExampleCanBackquote()
  301.     fmt.Println("### end test ExampleCanBackquote ###\n")

  302.     fmt.Println("---begin test ExampleFormatBool---")
  303.     ExampleFormatBool()
  304.     fmt.Println("### end test ExampleFormatBool ###\n")

  305.     fmt.Println("---begin test ExampleFormatFloat---")
  306.     ExampleFormatFloat()
  307.     fmt.Println("### end test ExampleFormatFloat ###\n")

  308.     fmt.Println("---begin test ExampleFormatInt---")
  309.     ExampleFormatInt()
  310.     fmt.Println("### end test ExampleFormatInt ###\n")

  311.     fmt.Println("---begin test ExampleFormatUint---")
  312.     ExampleFormatUint()
  313.     fmt.Println("### end test ExampleFormatUint ###\n")

  314.     fmt.Println("---begin test ExampleIsPrint---")
  315.     ExampleIsPrint()
  316.     fmt.Println("### end test ExampleIsPrint ###\n")

  317.     fmt.Println("---begin test ExampleItoa---")
  318.     ExampleItoa()

  319.     fmt.Println("---begin test ExampleParseBool---")
  320.     ExampleParseBool()

  321.     fmt.Println("---begin test ExampleParseFloat---")
  322.     ExampleParseFloat()

  323.     fmt.Println("---begin test ExampleParseInt---")
  324.     ExampleParseInt()

  325.     fmt.Println("---begin test ExampleParseUint---")
  326.     ExampleParseUint()

  327.     fmt.Println("---begin test ExampleQuote---")
  328.     ExampleQuote()

  329.     fmt.Println("---begin test ExampleQuoteRune---")
  330.     ExampleQuoteRune()

  331.     fmt.Println("---begin test ExampleQuoteRuneToASCII---")
  332.     ExampleQuoteRuneToASCII()

  333.     fmt.Println("---begin test ExampleQuoteToASCII---")
  334.     ExampleQuoteToASCII()

  335.     fmt.Println("---begin test ExampleUnquote---")
  336.     ExampleUnquote()

  337.     fmt.Println("---begin test ExampleUnquoteChar---")
  338.     ExampleUnquoteChar()

  339.     fmt.Println("---begin test ExampleNumError---
  340.     ExampleNumError()
    }
结果输出:
---begin test ExampleAppendBool---
bool:true
### end test ExampleAppendBool ###
---begin test ExampleAppendFloat---
float32:3.1415927E+00
float64:3.1415926535E+00
### end test ExampleAppendFloat ###
---begin test ExampleAppendInt---
int (base 10):-42
int (base 16):-2a
### end test ExampleAppendInt ###
---begin test ExampleAppendQuote---
quote:"\"Fran & Freddie's Diner\""
### end test ExampleAppendQuote ###
---begin test ExampleAppendQuoteRune---
rune:'?'
### end test ExampleAppendQuoteRune ###
---begin test ExampleAppendQuoteRuneToASCII---
rune (ascii):'\u263a'
### end test ExampleAppendQuoteRuneToASCII ###
---begin test ExampleAppendQuoteToASCII---
quote (ascii):"\"Fran & Freddie's Diner\""
### end test ExampleAppendQuoteToASCII ###
---begin test ExampleAppendUint---
uint (base 10):42
uint (base 16):2a
### end test ExampleAppendUint ###
---begin test ExampleAtoi---
int, 10### end test ExampleAtoi ###
---begin test ExampleCanBackquote---
true
false
### end test ExampleCanBackquote ###
---begin test ExampleFormatBool---
string, true
### end test ExampleFormatBool ###
---begin test ExampleFormatFloat---
string, 3.1415927E+00
string, 3.1415926535E+00
### end test ExampleFormatFloat ###
---begin test ExampleFormatInt---
string, -42
string, -2a
### end test ExampleFormatInt ###
---begin test ExampleFormatUint---
string, 42
string, 2a
### end test ExampleFormatUint ###
---begin test ExampleIsPrint---
true
false
### end test ExampleIsPrint ###
---begin test ExampleItoa---
string, 10
---begin test ExampleParseBool---
bool, true
---begin test ExampleParseFloat---
float64, 3.1415927410125732
float64, 3.1415926535
---begin test ExampleParseInt---
int64, -354634382
int64, -3546343826724305832
---begin test ExampleParseUint---
uint64, 42
uint64, 42
---begin test ExampleQuote---
"\"Fran & Freddie's Diner\t?\""
---begin test ExampleQuoteRune---
'?'
---begin test ExampleQuoteRuneToASCII---
'\u263a'
---begin test ExampleQuoteToASCII---
"\"Fran & Freddie's Diner\t\u263a\""
---begin test ExampleUnquote---
Unquote("\\\"Fran & Freddie's Diner\\t\\u263a\\\"\\\""): invalid syntax
Unquote("`\\\"Fran & Freddie's Diner\\t\\u263a\\\"\\\"`") = \"Fran & Freddie's Diner\t\u263a\"\"
Unquote("\"\\\"Fran & Freddie's Diner\\t\\u263a\\\"\\\"\"") = "Fran & Freddie's Diner ?""
Unquote("'\\u263a'") = ?
---begin test ExampleUnquoteChar---
value: "
multibyte: false
tail: Fran & Freddie's Diner\"
成功: 进程退出代码 0.


阅读(528) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~