Go 语言编程 — 数据类型转换
【摘要】 目录
文章目录
目录数据类型转换[]byte => other typeint => other typestring => other typeother type => stringarray => slice
数据类型转换
类型转换用于将一种数据类型的变量转换为另外一种类型的变量。
格式:
type_name...
目录
数据类型转换
类型转换用于将一种数据类型的变量转换为另外一种类型的变量。
格式:
type_name(expression)
[]byte => other type
注:[]byte 为字节流类型。
- []byte => string
string([]byte)
int => other type
- int => string
s = strconv.Itoa(i)
- int => int32
i32 = int32(num)
- int => int64
i64 = int64(num)
- int64/int32 => int
i = int(num)
- int64 => string
strconv.FormatInt(int64, 10)
- int64 => time.Duration
time.Duration(int64)
- int32 => byte
bytes.NewBuffer()
- int => float64
float64(num)
- other type -> int
int(int32/int64)
int(int64/int32)
string => other type
- string => int
strconv.Atoi(s)
- string => bool
strconv.ParseBool("true")
- string => float32
strconv.ParseFloat(s, 32)
- string => float64
strconv.ParseFloat(s, 64)
- string => uint
strconv.ParseUint()
- string => int32/int64
strconv.ParseInt(s, 10, 32/64)
- string(16进制) => int32/int64
strconv.ParseInt(s, 0, 32/64)
- string => []byte
[]byte(string)
other type => string
- int,int32,int64 => string
str1 := fmt.Sprintf("%d", i) // int/int32/int64
str3 := strconv.FormatInt(int64(i), 10) // int/int32/int64
str2 := strconv.Itoa(i) // int
- uint64 => string
strconv.FormatUint(unit64, 10)
- bool => string
strconv.FormatBool(true)
- float64 => string
strconv.FormatFloat(float64(12), 'f', -1, 64)
fmt.Sprintf("%.2f", float64)
array => slice
copy(array[:], slice[0:4])
copy(array[:], slice)
for index, b := range someSlice {
array[index] = b
}
文章来源: is-cloud.blog.csdn.net,作者:范桂飓,版权归原作者所有,如需转载,请联系作者。
原文链接:is-cloud.blog.csdn.net/article/details/107164554
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)