golang中字符串和数字转换


strconv.itoa()

数字转成字符串,再与字符串拼接

str := strconv.Itoa(12)
println("str + '23' = ", str+"23")

strconv.Atoi()

数字型字符串转换成数字,再进行四则运算

n, err := strconv.Atoi("123")
if err!=nil {
    panic(err)
}
println("12+n = ", 12+n)




// `结果`
12+n =  135
str + '23' =  1223

fmt.Sprintf()

func Sprintf(format string, a ...interface{}) string



str := fmt.Sprintf("123%s", "hello")
//输出:`123hello`

strings.Join()

func Join(a []string, sep string) string



str := []string{"hello", "world"}
s := strings.Join(str, " ")
//输出:`hello world`

strings.Builder–★

大量的字符串拼接,strings.Builder 是最好的方式。

var build strings.Builder
build.WriteString("123")
build.WriteString("ade")
build.WriteString("ade")
build.WriteString("ade")
println(build.String())

//输出:`123adeadeade`

buffer.WriteString

buffer := bytes.Buffer{}
buffer.WriteString("hello")
buffer.WriteString("golang")
println(buffer.String())

//输出:`hellogolang`


原文链接:https://blog.csdn.net/qq_40357974/article/details/115659419?spm=1001.2101.3001.6650.9&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-9.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-9.no_search_link