Black Friday Sale!
Save 50% on Web Development with Go and Test with Go until Dec 3.
Both courses have lifetime access, a 30-day money back guarantee, and free updates. This is also the biggest discount I offer on the courses, and I only do it once a year around Black Friday.
Thank you for your continued support and happy coding!
Jon Calhoun
Go 1.10 introduced the new strings.Builder
type, which can be used to efficiently and easily build or concatenate strings. In this post we explore some of the ways to use the type as well as why it implementing the io.Writer interface makes it an incredibly powerful tool. Most of this information could be found on the official docs, but the examples here are intended to get you up and running a tad faster while also illustrating some of the non-obvious implications.
The strings.Builder
documentation
If you are interested in checking out the official docs for the strings.Builder
type, you can do so here: https://golang.org/pkg/strings/#Builder
The most obvious use case, especially amongst beginners, is to simply want to concatenate some strings together. For instance, you may want to take all the elements of a slice and manually add them to a string. Without the strings.Builder
you might write some code like below.
func join(strs ...string) string {
var ret string
for _, str := range strs {
ret += str
}
return ret
}
While this works perfectly fine for a simple program, it is a little inefficient. Every time we call ret += str
a brand new string needs to be allocated in memory. This happens because strings in Go are immutable, so if we want to change a string or add contents to it we need to create an entirely new string. To avoid creating new strings as we build our final string, we can now use the strings.Builder
type along with its WriteString
method.
func join(strs ...string) string {
var sb strings.Builder
for _, str := range strs {
sb.WriteString(str)
}
return sb.String()
}
Similarly, you can use the WriteRune
and WriteByte
methods to add single characters to your string as you build it.
func joinRunes(runes ...rune) string {
var sb strings.Builder
for _, r := range runes {
sb.WriteRune(r)
}
return sb.String()
}
Once you are done building a specific string you can also reset your builder and then make use of it to build a new string.
func joinedAndReverse(strs ...string) (string, string) {
var sb strings.Builder
for _, str := range strs {
sb.WriteString(str)
}
joined := sb.String()
sb.Reset()
for i := len(strs) - 1; i >= 0; i-- {
sb.WriteString(strs[i])
}
return joined, sb.String()
}
string.Builder
also implements the io.Writer
interfaceIn addition to providing the WriteString
, WriteRune
, and WriteByte
methods, the string builder also implements the io.Writer interface. At first this may not seem very important - why would we want to write a byte slice when we could just write a string? - but because the string builder implements the io.Writer
interface it means that we can use functions like fmt.Fprintf
along with the string builder. This is demonstrated with the string builder example in the standard docs.
var b strings.Builder
for i := 3; i >= 1; i-- {
fmt.Fprintf(&b, "%d...", i)
}
b.WriteString("ignition")
fmt.Println(b.String())
Of course, you can always use the bytes.Buffer
that has been around and is discussed in the article, 6 Tips for Using Strings in Go.
Want to improve your Go skills?
Are you looking to practice Go, but can’t think of a good project to work on? Or maybe you just don’t know what techniques and skills you need to learn next, so you don't know where to start. If so, don’t worry - I’ve got you covered!
Gophercises is a FREE course where we work on exercise problems that are each designed to teach you a different aspect of Go. This includes topics ranging from basic string manipulation all the way to more advanced topics like functional options and concurrency. Each exercise has a sample solution, as well as a screencast (video) where I code the solution while walking you through the code. Plus, the Gophers are really cute 😉
Sign up for my mailing list and I'll send you a FREE sample from my course - Web Development with Go. The sample includes 19 screencasts and the first few chapters from the book.
You will also receive emails from me about Go coding techniques, upcoming courses (including FREE ones), and course discounts.
Jon Calhoun is a full stack web developer who teaches about Go, web development, algorithms, and anything programming. If you haven't already, you should totally check out his Go courses.
Previously, Jon worked at several statups including co-founding EasyPost, a shipping API used by several fortune 500 companies. Prior to that Jon worked at Google, competed at world finals in programming competitions, and has been programming since he was a child.
Related articles
Spread the word
Did you find this page helpful? Let others know about it!
Sharing helps me continue to create both free and premium Go resources.
Want to discuss the article?
See something that is wrong, think this article could be improved, or just want to say thanks? I'd love to hear what you have to say!
You can reach me via email or via twitter.
©2018 Jonathan Calhoun. All rights reserved.