go字符串转换




package main import (
"fmt" "strconv" ) /* 常用总结 1、str 转 int a, _ := strconv.Atoi("780") 2、int 转 str str = strconv.Itoa(8888) */ func main() { s := make([]byte, 0, 1024) s = strconv.AppendBool(s, true) s = strconv.AppendInt(s, 124, 10) 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', -1, 64) fmt.Println(str) //true124"hello world" // int 转 字符串 str = strconv.Itoa(8888) 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("780") fmt.Println(a) }
原文地址:https://www.cnblogs.com/lpwlpw/p/9999735.html