将 int 值附加到字符串

Append int value to string

我似乎找不到可行的解决方案: 情况如下:

var s string
n := 1

我想将 int 值附加到字符串 s。 然后在某个时候递增或递减 n 并再次附加新值

所以最后我会得到这样的字符串:

1213

到目前为止我尝试了什么:

s = s + string(rune(n)) // 出于某种原因 string(rune(n) is [] aka empty

您可以使用 strconv 包中的 strconv

package main

import (
    "fmt"
    "strconv"
)

func main() {
    a := 4 
    b := 3
    c := "1"

    fmt.Println(c + strconv.Itoa(a) + strconv.Itoa(b))
}

或者您可以使用 fmt 包中的 Sprintf:

package main

import (
    "fmt"
)

func main() {
    a := 4 
    b := 3
    c := "1"
    c = fmt.Sprintf("%s%d%d",c,a,b)

    fmt.Println(c + strconv.Itoa(a) + strconv.Itoa(b))
}