GoLang - Join Strings

In this tutorial, we will learn about how to join two strings using different approaches including Sprintf and Join.

Join Strings

String is a set of characters. Let us see how we can concatenate strings using four approaches.

Approach 1: Using '+' to join

'+' operator will concatenate two or more strings and return the concatenated string.

Syntax:

first_string+second_string+...

Example 1:

Let us join two strings.

CopiedCopy Code

package main

import "fmt"

func main() {
  
  // Consider two strings
    var first = "Welcome to"
    var second = "Gkindex"
 
    // Join the strings Using + operator
    fmt.Println("Joined: ", first+second)
}

Output:

CopiedCopy Code

Joined:  Welcome toGkindex

Example 2:

Let us join five strings.

CopiedCopy Code

package main

import "fmt"

func main() {
  
  // Consider five strings
    var first = "Welcome to "
    var second = "Gkindex "
    var third="GO "
    var forth="language"
    var fifth=" is useful."
 
    // Join the strings Using + operator
    fmt.Println("Joined: ", first+second+third+forth+fifth)
}

Output:

CopiedCopy Code

Joined:  Welcome to Gkindex GO language is useful.

Approach 2: Using Sprintf to join

It concatenates the strings using format specifier - '%s'.

Syntax:

fmt.Sprintf("%s%s...", first_string,second_string...)

Example: -

Let us join five strings.

CopiedCopy Code

package main

import "fmt"

func main() {
  
  // Consider five strings
    var first = "Welcome to "
    var second = "Gkindex "
    var third="GO "
    var forth="language"
    var fifth=" is useful."
 
    // Join the strings Using Sprintf().
    fmt.Println("Joined: ", fmt.Sprintf("%s%s%s%s%s", first,second,third,forth,fifth))
}

Output:

CopiedCopy Code

Joined:  Welcome to Gkindex GO language is useful.

Approach 3: Using Join() method

Join() is available in strings package which joins the strings present in an array by a separator.

It is important to import strings package.

Syntax:

strings.Join(strings_array,"separator")

Parameters: strings_array is the name of array that include strings separated by comma and the separator is a character which separated each string after join.

Example:

Let us consider five strings in an array and join them using a separator - "--".

CopiedCopy Code

package main

import ("fmt" 
"strings")

func main() {
  
  // Consider five strings in an array.
   var data= []string{"Welcome to ", "Gkindex ","GO ","language"," is useful."}
              
    // Join the strings Using Join() with a separator "--"
    fmt.Println("Joined: ", strings.Join(data,"--"))
}

Output:

CopiedCopy Code

Joined:  Welcome to --Gkindex --GO --language-- is useful.

Approach 4: Using '+=' operator to join

'+=' operator will join only two strings at a time. Second string is appended to the first string.

Syntax:

first_string+=second_string

Example:

Let us join two strings.

CopiedCopy Code

package main

import "fmt"

func main() {
  
  // Consider two strings
    var first = "Welcome to"
    var second = "Gkindex"
    
    // Join the strings Using += operator
  first+=second
    
    fmt.Println("Joined: ", first)
}

Output:

CopiedCopy Code

Joined:  Welcome toGkindex

We can see that two strings are concatenated and the joined string is stored in first string variable (Appended).

Conclusion

Now we know how to join the strings in Go Language using four approaches and understood the implementation with working examples.