golang 基本数据类型和string的相互转换
package main
import (
"fmt"
"strconv"
)
func main() {
var num1 int = 99
var num2 float64 = 123.12
var b bool = true
var myChar byte = 'h'
//方式一、int 转为 string
str := strconv.FormatInt(int64(num1), 10)
fmt.Printf("str type : %T , str=%q
", str, str)
//方式二、int 转为 string
str = strconv.Itoa(num1)
fmt.Printf("str type : %T , str=%q
", str, str)
//float64 转为 string
str = strconv.FormatFloat(num2, 'f', 10, 64)
fmt.Printf("str type : %T , str=%q
", str, str)
//bool 转为 string
str = strconv.FormatBool(b)
fmt.Printf("str type : %T , str=%q
", str, str)
//byte 转为 string
//str = strconv.Itoa(int(myChar))
str = string(myChar)
fmt.Printf("str type : %T , str=%q", str, str)
}
// str type : string , str="99"
// str type : string , str="99"
// str type : string , str="123.1200000000"
// str type : string , str="true"
// str type : string , str="h"