go语言学习--string、int、int64互相转换,字符串的截取,数组和字符串的转换

下面总结了go中常用的转换


#string到int

int,err:=strconv.Atoi(string)

#string到int64

int64, err := strconv.ParseInt(string, 10, 64) 

#int到string

string:=strconv.Itoa(int) 

#int64到string

string:=strconv.FormatInt(int64,10) 

#string转float

s := "3.1415926535"
v1, err := strconv.ParseFloat(v, 32)
v2, err := strconv.ParseFloat(v, 64)

#float转string

v := 3.1415926535
s1 := strconv.FormatFloat(v, 'E', -1, 32) //float32
s2 := strconv.FormatFloat(v, 'E', -1, 64) //float64

#float截取小数点的位数

func Decimal(value float64) float64 {
	value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", value), 64)
	return value
}

#判断map中的值是否存在

if _, ok := map[key]; ok {
//存在
}

#go中字符串的截取

res := strings.Split("heng,wowo", ",")

#go中数组转成字符串

proString := strings.Replace(strings.Trim(fmt.Sprint(ress), "[]"), " ", ",", -1)

#go中截取最后一位的字符串

pString = strings.Trim(pString, ",")

#go中计算字符串的长度

len([]rune(teacherInfoMap.Name))

转载于:https://www.cnblogs.com/ricklz/p/10005561.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值