作者:淘宝杂谈网z | 来源:互联网 | 2023-09-23 12:21
整形转string:**整形转字符串*funcIntToString(replyinterface{})(string,error){varstrstringswitchre
整形转string:
/**
整形转字符串
*/
func IntToString(reply interface{}) (string, error) {var str stringswitch reply := reply.(type) {//on breakcase int:intData := int64(reply)str = strconv.FormatInt(intData, 10)case int8:intData := int64(reply)str = strconv.FormatInt(intData, 10)case int16:intData := int64(reply)str = strconv.FormatInt(intData, 10)case int32:intData := int64(reply)str = strconv.FormatInt(intData, 10)case uint:intData := int64(reply)str = strconv.FormatInt(intData, 10)case uint8:intData := int64(reply)str = strconv.FormatInt(intData, 10)case uint16:intData := int64(reply)str = strconv.FormatInt(intData, 10)case uint32:intData := int64(reply)str = strconv.FormatInt(intData, 10)case uint64:intData := int64(reply)str = strconv.FormatInt(intData, 10)case string:return reply, nildefault:return "", errors.New("conversion failure")}return str, nil
}
字符串转切片:
//字符串转切片
str := "1,a,11,2"
slices :=strings.Split(str, ",")
fmt.Println(slices) //["1","a","11","2"]//切片转字符串
str1 := strings.Join(slices,",")
fmt.Println(str1) //"1,a,11,2"