Repeatedly concatenating strings with += can become expensive: because strings are immutable, each iteration reallocates a result and copies existing content. strings.Builder accumulates bytes internally, reducing repeated memory copying. section 5.4
Replace repeated += concatenation
Use the zero value, append each value with WriteString, and obtain the result with String: func concat(values []string) string { var sb strings.Builder for _, value := range values { _, _ = sb.WriteString(value) } return sb.String() } WriteString returns an error only to satisfy io.StringWriter; this implementation does not produce a non-nil error. Builder also accepts bytes through Write, one byte through WriteByte, and one rune through WriteRune. section 5.4
Choose the implementation according to input size, known capacity, and execution context:
- Call Grow when the total byte length is known. Sum len(value) first, call sb.Grow(total), then write the values to reduce further allocation. section 5.4
- Do not share a Builder between concurrent operations; its internal byte slice is appended to, so unsynchronized calls can race. section 5.4


