From 708329d8f21e8e5af7d9277fdacf41e6411b7115 Mon Sep 17 00:00:00 2001 From: Apin <81305669+qjksxy@users.noreply.github.com> Date: Wed, 17 Jul 2024 22:45:13 +0800 Subject: [PATCH] doc: update golang.md (#802) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 类型转换中补充了字符串与其他类型相互转换的方法 --- docs/golang.md | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/docs/golang.md b/docs/golang.md index 522e2ae7dff..07d152b2b6d 100644 --- a/docs/golang.md +++ b/docs/golang.md @@ -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 字符串