parent
fac5af10c8
commit
708329d8f2
@ -279,13 +279,26 @@ u := uint(i)
|
|||||||
s := string(i)
|
s := string(i)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 如何获取int字符串?
|
#### 字符串与其他类型的相互转换
|
||||||
|
|
||||||
```go
|
```go
|
||||||
i := 90
|
// 字符串转其他类型
|
||||||
// 需要导入“strconv”
|
str := "90"
|
||||||
s := strconv.Itoa(i)
|
// 整数类型
|
||||||
fmt.Println(s) // Outputs: 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 字符串
|
Golang 字符串
|
||||||
|
Loading…
x
Reference in New Issue
Block a user