New Way:
From Go 1.10 there is a strings.Builder type, please take a look at this answer for more detail.
Old Way:
Use the bytes package. It has a Buffer type which implements io.Writer.
package main
import (
"bytes"
"fmt"
)
func main() {
var buffer bytes.Buffer
for i := 0; i < 1000; i++ {
buffer.WriteString("a")
}
fmt.Println(buffer.String())
}
This does it in O(n) time.
Answer from marketer on Stack OverflowNew Way:
From Go 1.10 there is a strings.Builder type, please take a look at this answer for more detail.
Old Way:
Use the bytes package. It has a Buffer type which implements io.Writer.
package main
import (
"bytes"
"fmt"
)
func main() {
var buffer bytes.Buffer
for i := 0; i < 1000; i++ {
buffer.WriteString("a")
}
fmt.Println(buffer.String())
}
This does it in O(n) time.
In Go 1.10+ there is strings.Builder, here.
A Builder is used to efficiently build a string using Write methods. It minimizes memory copying. The zero value is ready to use.
Example
It's almost the same with bytes.Buffer.
package main
import (
"strings"
"fmt"
)
func main() {
// ZERO-VALUE:
//
// It's ready to use from the get-go.
// You don't need to initialize it.
var sb strings.Builder
for i := 0; i < 1000; i++ {
sb.WriteString("a")
}
fmt.Println(sb.String())
}
Click to see this on the playground.
Supported Interfaces
strings.Builder's methods are being implemented with the existing interfaces in mind so that you can switch to the new Builder type easily in your code.
| Method Signature | Interface | Description |
|---|---|---|
Grow(int) |
bytes.Buffer |
Grows the buffer's capacity by the specified amount. See bytes.Buffer#Grow for more information. |
Len() int |
bytes.Buffer |
Returns the number of bytes in the buffer. See bytes.Buffer#Len for more information. |
Reset() |
bytes.Buffer |
Resets the buffer to be empty. See bytes.Buffer#Reset for more information. |
String() string |
fmt.Stringer |
Returns the contents of the buffer as a string. See fmt.Stringer for more information. |
Write([]byte) (int, error) |
io.Writer |
Writes the given bytes to the buffer. See io.Writer for more information. |
WriteByte(byte) error |
io.ByteWriter |
Writes the given byte to the buffer. See io.ByteWriter for more information. |
WriteRune(rune) (int, error) |
bufio.Writer or bytes.Buffer |
Writes the given rune to the buffer. See bufio.Writer#WriteRune or bytes.Buffer#WriteRune for more information. |
WriteString(string) (int, error) |
io.stringWriter |
Writes the given string to the buffer. See io.stringWriter for more information. |
Differences from bytes.Buffer
- It can only grow or reset.
- It has a
copyCheckmechanism built-in that prevents accidentally copying it. Inbytes.Buffer, one can access the underlying bytes like this:(*Buffer).Bytes().strings.Builderprevents this problem. Sometimes, this is not a problem, though, and is desired instead. For example: For the peeking behavior when the bytes are passed to anio.Readeretc. bytes.Buffer.Reset()rewinds and reuses the underlying buffer whereas thestrings.Builder.Reset()does not, it detaches the buffer.
Note
- Do not copy a
strings.Buildervalue as it caches the underlying data. - If you want to share a
strings.Buildervalue, use a pointer to it.
Check out its source code for more details, here.