We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1372ac6 commit 400387aCopy full SHA for 400387a
01-go_basic/1-8-Strings/09-字符串转换.go
@@ -0,0 +1,34 @@
1
+package main
2
+
3
+// https://studygolang.com/pkgdoc(Go语言标准库文档)
4
5
+import (
6
+ "fmt"
7
+ "strconv"
8
+)
9
10
+func main() {
11
+ // 将bool类型转换为字符串类型
12
+ str1 := strconv.FormatBool(true)
13
+ fmt.Println(str1)
14
15
+ // 将int类型转换为字符串类型
16
+ str2 := strconv.Itoa(123)
17
+ fmt.Println(str2)
18
19
+ // 将字符串转换为bool类型
20
+ b, err := strconv.ParseBool("true") // ParseBool:解析(或转换)一个字符串,判断这个字符串是否代表布尔值(即真或假),并将其转换成程序中的布尔类型(bool)
21
+ if err != nil {
22
+ fmt.Println(err)
23
+ } else {
24
+ fmt.Println(b)
25
+ }
26
27
+ // 将字符串转换为int类型
28
+ num, err := strconv.Atoi("123") // Atoi:"ASCII to Integer",意即“从ASCII码转换到整数”
29
30
31
32
+ fmt.Println(num)
33
34
+}
0 commit comments