
正文
go字符串转换
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
package mainimport (
"fmt"
"strconv"
)/* 常用总结1、str 转 int a, _ := strconv.Atoi("780")
2、int 转 str str = strconv.Itoa(8888)
*/func main() { s := make([]byte, , )
s = strconv.AppendBool(s, true)
s = strconv.AppendInt(s, , )
s = strconv.AppendQuote(s, "hello world") fmt.Println(string(s)) //true124"hello world" // 其他类型转换为字符串 var str string
str = strconv.FormatBool(false) // f 打印格式 以小数方式,-1指小数点位数 64 float64 处理
str = strconv.FormatFloat(3.14, 'f', -, )
fmt.Println(str) //true124"hello world" // int 转 字符串
str = strconv.Itoa()
fmt.Println(str) //888 // 字符串转其他类型
var flag bool
var err error flag, err = strconv.ParseBool("true")
if err == nil { fmt.Println(flag) } else {
fmt.Println(err) } // str 转int
a, _ := strconv.Atoi("")
fmt.Println(a)}







