Your benchmark is incorrect, the only thing you are measuring is the optimizations of the golang compiler / runtime in simple cases. Here is a slightly improved benchmark that shows how hugely different results you get if you disable golang optimizations (or make it hard for go to optimize your code):
As you can see a Builder is often more than 1000x faster than other approaches. fmt.Sprintf and strings.Join have about the same speed as +, but this changes as soon as you do multiple concatenations in a single call:
Your benchmark is incorrect, the only thing you are measuring is the optimizations of the golang compiler / runtime in simple cases. Here is a slightly improved benchmark that shows how hugely different results you get if you disable golang optimizations (or make it hard for go to optimize your code):
If executed with
go test -gcflags=-N -bench=.returns the following results:As you can see a Builder is often more than 1000x faster than other approaches.
fmt.Sprintfandstrings.Joinhave about the same speed as+, but this changes as soon as you do multiple concatenations in a single call:here
strings.Joinwill be measurable faster than+.