1.用字符串连接切片元素(类似php implode)
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
data := []int{104, 101, 108, 108, 111}
fmt.Println(IntSliceToString(data, ","))
}
func IntSliceToString(intSlice []int, sep string) string {
strBuilder := new(strings.Builder)
for i, intValue := range intSlice {
if i > 0 {
strBuilder.WriteString(sep)
}
strBuilder.WriteString(strconv.Itoa(intValue))
}
return strBuilder.String()
}
运行结果: