golang类型转化

 int 转 float
var sum int =  mean:= float32(sum)
 
float 转 int
a := 5.0
b := int(a)
 
string 转 int
i,_ := strconv.Atoi("")
fmt.println( + i) //
 
int 转 string
fmt.println("a" + strconv.Itoa()) // a32
interface 转 string
var v1 interface{}="asdf"
fmt.Println(v1.(string))
 
interface 转 int
var v2 interface{}=
fmt.Println(v2.(int)+) //
 
string 转 []byte
var str string = "test"
var b []byte = []byte(str)
 
[]byte 转 string
var str string = string(b)
var str string = string(b[:]) //or
对象转json字符串
str,err:=json.Marshal(stu)
json字符串转对象
stb := &Student{}
err = json.Unmarshal([]byte(strData), &stb)
Map转json
map1 := make(map[string]interface{})
map1[""] = "hello"
map1[""] = "world"
str, err := json.Marshal(map1) //return []byte
fmt.Println("map to json", string(str))
 
json转map
map2 := make(map[string]interface{})
err = json.Unmarshal([]byte(jsonStr), &map2)
 
数组转string
str := string(arr[:])
 
string 转 数组
s = strings.Split("abc,abc", ",")
fmt.Println(s, len(s)) //[abc abc] 2
 
上一篇:SpringMVC异常处理机制详解[附带源码分析]


下一篇:MySQL重置root用户密码的方法