1 int与string
备注:string不可直接转int32,先转为int,再转成int32
package main import ( "fmt" "strconv" ) func main() { //int 转 string var a int = 3 str := strconv.Itoa(a) fmt.Printf("str:类型:%T 值:%s\n", str, str) var aa string = "2" aaa, _ := strconv.Atoi(aa) fmt.Printf("aaa:类型:%T 值:%d\n", aaa, aaa) //int32 转 string var b int32 = 3 str32 := fmt.Sprint(b) fmt.Printf("str32:类型:%T 值:%s\n", str32, str32) var bb string = "2" bbb, _ := strconv.Atoi(bb) bbbb := int32(bbb) fmt.Printf("bbbb:类型:%T 值:%d\n", bbbb, bbbb) //int64转string var c int64 = 3 str64 := strconv.FormatInt(c, 10) fmt.Printf("str64:类型:%T 值:%s\n", str64, str64) var cc string = "2" ccc, _ := strconv.ParseInt(cc, 10, 64) fmt.Printf("ccc:类型:%T 值:%d\n", ccc, ccc) }
2 float与string
备注:string不可直接转float32,先转成float64,再转成float32
package main import ( "fmt" "strconv" ) func main() { //float32转string var d float32 = 0.998 flo32 := fmt.Sprintf("%f", d) fmt.Printf("flo32:类型:%T 值:%s\n", flo32, flo32) var dd string = "0.998" ddd, _ := strconv.ParseFloat(dd, 32) dddd := float32(ddd) fmt.Printf("dddd:类型:%T 值:%f\n", dddd, dddd) //float64转string var e float64 = 0.998 flo64 := strconv.FormatFloat(e, 'f', -1, 32) fmt.Printf("flo64:类型:%T 值:%s\n", flo64, flo64) var ee string = "0.998" eee, _ := strconv.ParseFloat(ee, 64) fmt.Printf("ddd:类型:%T 值:%f\n", eee, eee) }
标签:类型转换,string,--,fmt,strconv,var,Printf,go,float32 From: https://www.cnblogs.com/Xinenhui/p/16889430.html