String concatenation
It is generally more efficient to use the bytes.Buffer
type to build strings rather than concatenating strings using the +
operator.
Look at this poor performance code:
s := ""
for i := 0; i < 100000; i++ {
s += "x"
}
fmt.Println(s)
This code will create a new string on each iteration of the loop, which can be inefficient and may lead to poor performance.
Instead, you can use the strings.Builder
to build the string more efficiently:
var builder strings.Builder
for i := 0; i < 100000; i++ {
builder.WriteString("x")
}
s := builder.String()
fmt.Println(s)