strconv
Format :基本类型转string型
package main
import ( "fmt" "strconv" )
func main() {
var i int8 = 73
var e float64 = 7365.00
var a bool = true
var s string
s = strconv.FormatInt(int64(i), 10) //返回十进制
fmt.Printf("s %T,=%q\n", s, s)
s = strconv.FormatFloat(e, 'f', 7, 64)
fmt.Printf("s,%T,=%q\n", s, s)
s = strconv.FormatBool(a)
fmt.Printf("s %T,=%q\n", s, s)
}
Itoa :整形转string型
Parse :string转基本类型
package main
import (
"fmt"
"strconv"
)
func main() {
var s string = "true"
var b bool //对应string
/* b,_=strconv.ParseBool(s)
说明
1.strconv.ParseBool(str)
函数会返回两个值(value bool,err error)
2.因为我只想获取到va1ue boo1,不想获取err所以我使用_忽略*/
b, _ = strconv.ParseBool(s)
fmt.Printf("b type=%T,value=%v\n", b, b)
var s1 string = "7563"
var i int64
i, _ = strconv.ParseInt(s1, 10, 64) //十进制,64位(系统固定好了)
fmt.Printf("i type=%T,value=%v\n", i, i)
var s2 string = "73.65" //对应float64,所以不能写中文
var f float64
f, _ = strconv.ParseFloat(s2, 64) //64位(系统固定好了)
fmt.Printf("f type=%T,value=%v\n", f, f)
}