Golang:string 与 strconv.Itoa 的区别
Example
我们来尝试打印 0 到 100 的对应结果:
1 | package main |
结果:
说明:结果剔除了一些不可见元素
i | string | strconv.Itoa |
---|---|---|
33 | ! | 33 |
34 | “ | 34 |
35 | # | 35 |
36 | $ | 36 |
37 | % | 37 |
38 | & | 38 |
39 | ‘ | 39 |
40 | ( | 40 |
41 | ) | 41 |
42 | * | 42 |
43 | + | 43 |
44 | , | 44 |
45 | - | 45 |
46 | . | 46 |
47 | / | 47 |
48 | 0 | 48 |
49 | 1 | 49 |
50 | 2 | 50 |
51 | 3 | 51 |
52 | 4 | 52 |
53 | 5 | 53 |
54 | 6 | 54 |
55 | 7 | 55 |
56 | 8 | 56 |
57 | 9 | 57 |
58 | : | 58 |
59 | ; | 59 |
60 | < | 60 |
61 | = | 61 |
62 | > | 62 |
63 | ? | 63 |
64 | @ | 64 |
65 | A | 65 |
66 | B | 66 |
67 | C | 67 |
68 | D | 68 |
69 | E | 69 |
70 | F | 70 |
71 | G | 71 |
72 | H | 72 |
73 | I | 73 |
74 | J | 74 |
75 | K | 75 |
76 | L | 76 |
77 | M | 77 |
78 | N | 78 |
79 | O | 79 |
80 | P | 80 |
81 | Q | 81 |
82 | R | 82 |
83 | S | 83 |
84 | T | 84 |
85 | U | 85 |
86 | V | 86 |
87 | W | 87 |
88 | X | 88 |
89 | Y | 89 |
90 | Z | 90 |
91 | [ | 91 |
92 | \ | 92 |
93 | ] | 93 |
94 | ^ | 94 |
95 | _ | 95 |
96 | ` | 96 |
97 | a | 97 |
98 | b | 98 |
99 | c | 99 |
100 | d | 100 |
ASCII
我摘录了点百度的 ASCII 编码
总结
string
实际是将int
通过ASCII
转换为对应的字符strconv.Itoa
则是将int
转换为 字符串类型