Intern
What will happen if you add strings?
sobes.tech AI
Answer from AI
Concatenation of strings in Go occurs by joining them into a single string.
s1 := "hello"
s2 := " world"
s3 := s1 + s2 // s3 becomes "hello world"
This operation creates a new string. For very frequent concatenations in a loop, it is recommended to use strings.Builder for better performance, as concatenation with the + operator can lead to multiple memory allocations.
import "strings"
var builder strings.Builder
builder.WriteString("part 1")
builder.WriteString(" part 2")
result := builder.String() // result becomes "part 1 part 2"