Skip to content

Commit 400387a

Browse files
authored
strings (#54)
1 parent 1372ac6 commit 400387a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
if err != nil {
30+
fmt.Println(err)
31+
} else {
32+
fmt.Println(num)
33+
}
34+
}

0 commit comments

Comments
 (0)