How to merge multiple strings and int into a single string
You can use strings.Join, which is almost 3x faster than fmt.Sprintf. However it can be less readable.
output := strings.Join([]string{"key:", "value", ", key2:", strconv.Itoa(100)}, "")
See https://play.golang.org/p/AqiLz3oRVq
strings.Join vs fmt.Sprintf
BenchmarkFmt-4 2000000 685 ns/op
BenchmarkJoins-4 5000000 244 ns/op
Buffer
If you need to merge a lot of strings, I'd consider using a buffer rather than those solutions mentioned above.
I like to use fmt's Sprintf
method for this type of thing. It works like Printf
in Go or C only it returns a string. Here's an example:
output := fmt.Sprintf("%s%s%s%d", "key:", "value", ", key2:", 100)
Go docs for fmt.Sprintf
You can simply do this:
package main
import (
"fmt"
"strconv"
)
func main() {
result:="str1"+"str2"+strconv.Itoa(123)+"str3"+strconv.Itoa(12)
fmt.Println(result)
}
Using fmt.Sprintf()
var s1="abc"
var s2="def"
var num =100
ans:=fmt.Sprintf("%s%d%s", s1,num,s2);
fmt.Println(ans);