doc: update golang.md (#802)

类型转换中补充了字符串与其他类型相互转换的方法
This commit is contained in:
Apin 2024-07-17 22:45:13 +08:00 committed by GitHub
parent fac5af10c8
commit 708329d8f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -279,13 +279,26 @@ u := uint(i)
s := string(i)
```
#### 如何获取int字符串
#### 字符串与其他类型的相互转换
```go
i := 90
// 需要导入“strconv”
s := strconv.Itoa(i)
fmt.Println(s) // Outputs: 90
// 字符串转其他类型
str := "90"
// 整数类型
i, err := strconv.Atoi(str)
if err != nil {
fmt.Println("转换错误:", err)
} else {
fmt.Println(i)
}
// 浮点类型
f, err := strconv.ParseFloat(str, 64)
// []byte 类型
bytes := []byte(str)
// 其他类型转字符串
str = strconv.Itoa(i)
str = strconv.FormatFloat(f, 'f', 2, 64)
str = string(bytes[:])
```
Golang 字符串