Strings
You should never concatenate Go strings in a loop. We’ve written this function here only for teaching purposes.
In Go, strings are immutable and constant. When we use the + operator on two strings, Go will allocate memory for a new string of the correct size and copy the bytes of each operand into that new string.
When the number of strings to connect exceeds two, there are two quite common alternatives that are worth checking:
strings.Joinstrings.Builder
Under the hood, a strings.Builder stores the characters in a slice of runes, which is a lot easier to grow than a rock-solid string.
This type exposes several methods that can be used to append characters to the string being built: WriteString, WriteRune, WriteByte, and the basic Write, which takes a slice of bytes
Once we’re done feeding data to the builder, calling String() on it will return the final string.