How to Concatenate Strings in Go

Hey! If you love Go and building Go apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Want to learn how to build better Go applications faster and easier? You can.

Check out my course on the Go Standard Library. You can check it out now for free.


Learn how to concatenate strings in Go with the most efficient techniques.

Concatenating strings is a common task in programming, and it’s important to do it efficiently to avoid performance issues. In this article, we will explore different techniques for concatenating strings in Go, and you will learn how to use them effectively.

  1. Using the + Operator
    The most basic way to concatenate strings in Go is by using the + operator. Here’s an example:
a := "Hello"
b := "World"
c := a + b
fmt.Println(c) // Output: HelloWorld

This method is simple and straightforward, but it can be slow for large strings. The reason is that Go creates a new string object in memory each time you use the + operator. This can lead to performance issues if you are concatenating many strings together.

  1. Using fmt.Sprintf()
    Another way to concatenate strings in Go is by using the fmt.Sprintf() function. Here’s an example:
a := "Hello"
b := "World"
c := fmt.Sprintf("%s%s", a, b)
fmt.Println(c) // Output: HelloWorld

This method is faster than using the + operator because it only creates one string object in memory instead of many. However, this method can be more complex to read and write compared to the + operator.

  1. Using strings.Join()
    The strings package in Go provides a Join() function that allows you to concatenate multiple strings together with a separator between them. Here’s an example:
a := "Hello"
b := "World"
c := "!"
d := strings.Join([]string{a, b, c}, "")
fmt.Println(d) // Output: HelloWorld!

This method is faster and more efficient than using the + operator or fmt.Sprintf() because it only creates one string object in memory instead of many. However, this method can be less readable compared to the previous two methods.

  1. Using strings.Builder()
    The strings package also provides a Builder type that allows you to concatenate multiple strings together efficiently without creating unnecessary objects. Here’s an example:
a := "Hello"
b := "World"
c := "!"
d := &strings.Builder{}
d.WriteString(a)
d.WriteString(b)
d.WriteString(c)
fmt.Println(d.String()) // Output: HelloWorld!

This method is the most efficient way to concatenate strings in Go because it only creates one string object in memory instead of many. However, this method can be more complex to read and write compared to the previous three methods.

  1. Using a Custom Type
    If you are concatenating multiple strings together frequently, you may want to create a custom type that allows you to do so efficiently. Here’s an example:
type StringBuilder struct {
	strings []string
}

func (sb *StringBuilder) Write(str string) {
	sb.strings = append(sb.strings, str)
}

func (sb *StringBuilder) String() string {
	return strings.Join(sb.strings, "")
}

With this custom type, you can concatenate multiple strings together efficiently without creating unnecessary objects. Here’s an example:

a := "Hello"
b := "World"
c := "!"
d := &StringBuilder{}
d.Write(a)
d.Write(b)
d.Write(c)
fmt.Println(d.String()) // Output: HelloWorld!

This method is the most efficient way to concatenate strings in Go because it only creates one string object in memory instead of many. However, this method requires more code and may be less readable compared to the previous five methods.

In conclusion, there are several ways to concatenate strings in Go with different trade-offs between efficiency and readability. The best approach depends on your specific use case and personal preferences. It’s important to choose the most efficient technique for your specific needs to avoid performance issues and ensure your code is readable and maintainable.



Stay up to date on the latest in Coding for AI and Data Science

Intuit Mailchimp