There's a strings.Repeat function in the standard library, so strings.Repeat(" ", n)
would do. The implementation uses a strings.Builder
like yours, except:
It calls Grow
on the builder ahead of time to guarantee only one allocation, instead of possibly reallocating several times in the middle.
It progressively doubles the content of the builder instead of calling WriteString
n times. Presumably they do that because it's more efficient.
For reasonable numbers of spaces, none of that probably makes any difference... but anyway, there's a stdlib function that expresses what you mean (really it's the direct equivalent of Python's str * int), so you might as well use it.