W3Basic Logo

Concatenate Strings in Go

String concatenation is one of the core concepts of any programming language. In Golang, there are different approaches available to concatenate strings.

  1. Using + and += Operators
  2. Using strings.Builder
  3. Using Join() Method
  4. Using bytes.Buffer
  5. Using Sprintf() Method

Let us explore each of these approaches in-depth with examples.


1) Using + and += Operators

Like most programming languages, we can use the + and the += operators to concatenate the strings. It is the simplest way and most elegant way to join two or more strings.

package main

import "fmt"

func main() {
    text1 := "Golang"
    text2 := "is Awesome"
    output := text1 + " " + text2
    fmt.Println("Concatenated String is:", output)

}

Output

Concatenated String is: Golang is Awesome

The += operator, also known as the compound assignment operator, is commonly used for incrementing values. The += operator appends the right side value to the variable, as shown below.

package main

import "fmt"

func main() {
    text := "Welcome "
    text += "to "
    text += "Golang Tutorial"
    fmt.Println("Concatenated String is:", text)

}

Output

Concatenated String is: Welcome to Golang Tutorial

2) Using strings.Builder

The strings.Builder was released in Go 1.10 version. String Builder is the most efficient way to build a string using Write methods in Go. Moreover, it is the most optimized way as it minimizes memory copying.

We use the WriteString() method which allows us to concatenate strings in the most optimized and efficient way.

package main

import (
    "fmt"
    "strings"
)

func main() {
    str1 := "Golang"
    str2 := "is Awesome"
    var sb strings.Builder
    sb.WriteString(str1)
    sb.WriteString(" ")
    sb.WriteString(str2)
    sb.WriteString("!!!")
    fmt.Println("Concatenated String is:", sb.String())

}

Output

Concatenated String is: Golang is Awesome!!!

3) Using Join() Method

The strings package has a Join() method that can be used to concatenate strings.

Syntax

func Join(elems []string, sep string) string

The Join() method takes two arguments:

  • elems - A slice of string
  • sep - The separator that needs to be used while concatenating the strings.
package main

import (
    "fmt"
    "strings"
)

func main() {
    s := []string{"Go", "is", "Awesome"}
    fmt.Println("Concatenated String is:", strings.Join(s, " "))

    lang := []string{"Go", "Python", "C#", "Java"}
    fmt.Println("Concatenated String with comma:", strings.Join(lang, ","))

}

Output

Concatenated String is: Go is Awesome
Concatenated String with comma: Go,Python,C#,Java

4) Using bytes.Buffer

The strings.Builder was available in Go 1.10 version and above. In the earlier version, we can use thebytes package with a Buffer type and utilize the WriteString() method to concatenate the strings.

The bytes.Buffer is also an optimized and efficient way to concatenate the strings if you use the old Go version.

package main

import (
    "bytes"
    "fmt"
)

func main() {
    str1 := "Golang"
    str2 := "is Awesome"
    var buffer bytes.Buffer
    buffer.WriteString(str1)
    buffer.WriteString(" ")
    buffer.WriteString(str2)
    buffer.WriteString("!!!")
    fmt.Println("Concatenated String is:", buffer.String())

}

Output

Concatenated String is: Golang is Awesome!!!

5) Using Sprintf() Method

We can also concatenate strings using the string formatting functions such as the fmt.Sprintf() method available in the fmt package.

package main

import (
    "fmt"
)

func main() {
    str1 := "Golang"
    str2 := "is Awesome"

    output := fmt.Sprintf("%s %s", str1, str2)
    fmt.Println("Concatenate using Sprintf:", output)

}

Output

Concatenate using Sprintf: Golang is Awesome

© 2023 W3Basic. All rights reserved.

Follow Us: