Home
Search
Golang strings.Builder ExamplesUse the strings.Builder type and the WriteString func to append strings. Benchmark strings.Builder.
dot net perls
Strings.Builder. Often programs must append many strings to form a single, larger string. This can cause some performance issues.
With a special type in Go, we can append strings faster. Fewer allocations are needed. The appends are performed faster. The strings.Builder type is used here.
An example. Here we create a new strings.Builder. Please note that Go 1.10 or later is required to run this program. A compile-time error occurs on earlier versions.
WriteString This func appends the argument string to the strings.Builder instance.
String This method converts the buffered data in the Builder into a Go string.
Golang program that uses strings.Builder
package main import ( "fmt" "strings" ) func main() { // Create new Builder. builder := strings.Builder{} // Write a string 5 times. for i := 0; i < 5; i++ { builder.WriteString("bird ") } // Convert Builder to String and print it. result := builder.String() fmt.Println(result) }
bird bird bird bird bird
Builder argument. To pass the strings.Builder to another func, and have that func call WriteString to add to the builder, we can pass a reference to the strings.Builder. We use the "&" char.
Golang program that passes strings.Builder as argument
package main import ( "fmt" "strings" ) func Append2Strings(builder *strings.Builder) { builder.WriteString("ABC") builder.WriteString("XYZ") } func main() { builder := strings.Builder{} // Pass as reference to use it in another method. Append2Strings(&builder) fmt.Println(builder.String()) }
ABCXYZ
Grow. This is an important performance optimization for strings.Builder in Go programs. If we know an approximate size of the entire data, we can use a capacity.
Tip Call Grow() to accommodate the entire length of the final data. Fewer resizes will be required.
Info Calling Grow() can lead to measurable performance improvements in real-world programs.
Golang program that uses Grow
package main import ( "fmt" "strings" ) func main() { builder := strings.Builder{} // Grow to a larger size to reduce future resizes of the buffer. builder.Grow(30000) builder.WriteString("Test") fmt.Println(builder.String()) }
Test
Benchmark. Why should we use strings.Builder instead of string appends? The answer is performance. We can append strings faster with a strings.Builder.
Version 1 This version of the code uses the strings.Builder and calls WriteString many times.
Version 2 Here we append a string with the plus operator. This code causes allocations.
Result In a long-running loop, it is much faster to append string data to a strings.Builder than use strings.
Golang program that benchmarks strings.Builder
package main import ( "fmt" "strings" "time" ) func main() { t0 := time.Now() // Version 1: use Builder with WriteString. for i := 0; i < 100000; i++ { builder := strings.Builder{} for v := 0; v < 100; v++ { builder.WriteString("hello") } if builder.Len() == 0 { break } } t1 := time.Now() // Version 2: use string appends. for i := 0; i < 100000; i++ { result := "" for v := 0; v < 100; v++ { result += "hello" } if len(result) == 0 { break } } t2 := time.Now() // Results. fmt.Println(t1.Sub(t0)) fmt.Println(t2.Sub(t1)) }
142.232646ms strings.Builder 1.234626415s string +=
A summary. With strings.Builder and WriteString() we see an early 10x speedup in a simple benchmark. This type can improve the performance of many programs.
Home
© 2007-2021 sam allen. see site info on the changelog