Golang Program to demonstrate the string concatenation


What is String Concatenation?

In Go, string concatenation is the process of combining two or more strings into a single string. There are several ways to concatenate strings in Go. We shall discuss them in this article.

Method 1: By Using User-Defined Function

The function that we create will take two strings as arguments and will return the resultant string by combining both of them as the result.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Then create a function that will join the two strings together. The function accepts two strings to be concatenated and returns the final string formed after joining both them together by using ‘+’ operator.

  • Step 3 − Then, start the main() function. Inside the main() initialize two variables of type strings and assign value to them. Further, print the string on the screen.

  • Step 4 − Call the concatenation() function by passing the two strings as arguments to it and store the result obtained in a variable called string.

  • Step 5 − Now, we need to print the final result on the screen by using fmt.Println() function.

Example

In this example we will use an external function in go programming language to show how two strings can be joined together to form a single string.

package main
import "fmt"

// function to join two strings together
func concatenate(s1, s2 string) string {
   return s1 + s2
}
func main() {
   var s1 string = "This is the first string, "
   var s2 string = "This is the second string"
   fmt.Println("The first string is:\n", s1)
   fmt.Println()
   fmt.Println("The second string is:\n", s2)
   result := concatenate(s1, s2)
   fmt.Println()
   fmt.Println("The final string obtained by concatenating the above strings together is:\n", result)
}

Output

The first string is:
 This is the first string, 

The second string is:
 This is the second string

The final string obtained by concatenating the above strings together is:
 This is the first string, This is the second string

Method 2: By Using Internal Package

In this method, we will write a go language program to demonstrate the string concatenation by using internal function.

Syntax

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

The join function is used to convert an array to string. This function is present in strings package. it takes two arguments first one is the array that we wish to convert and second is the separation with which the array elements should be separated once they are converted to strings and returns the final string.

buffer.WriteString() 

The WriteString() function is present in bytes package. this function allows us to write the string characters in the buffer memory. The function does not modify the string if we call it multiple types. It rather updates the string by concatenating it with the next string and so on.

Algorithm

  • Step 1 − First, we need to import the fmt and strings package.

  • Step 2 − Now, start the main() function. Inside this function initialize two string variables and assign values to them.

  • Step 3 − Print these strings on the screen.

  • Step 4 − Now, use the internal function for string concatenation

  • Step 5 − The function then returns the string after combining both the strings together as a result.

  • Step 6 − Store the result obtained in a variable and print it on the screen by using fmt.Println() function.

Example 1

Following is the example, to demonstrate string concatenation by using strings.Join() function.

package main
import (
   "fmt"
   "strings"
)

func main() {
   var s1 string = "Life is what happens\t"
   var s2 string = "when you're busy making other plans."
   fmt.Println("The first string is:\n", s1)
   fmt.Println()
   fmt.Println("The second string is:\n", s2)
   var result string = strings.Join([]string{s1, s2}, "")
   fmt.Println()
   fmt.Println("The final string obtained by concatenating the above strings together is:\n", result)
}

Output

The first string is:
 Life is what happens	

The second string is:
 when you're busy making other plans.

The final string obtained by concatenating the above strings together is:
 Life is what happens	when you're busy making other plans.

Example 2

In this example, we will write a go language program to demonstrate the string concatenation by using bytes package.

package main
import (
   "bytes"
   "fmt"
)

func main() {
   var buffer bytes.Buffer
   var s1 string = "Life is what happens\t"
   var s2 string = "when you're busy making other plans."
   fmt.Println("The first string is:\n", s1)
   fmt.Println()
   fmt.Println("The second string is:\n", s2)
   buffer.WriteString(s1)
   buffer.WriteString(s2)

   var result string = buffer.String()
   fmt.Println()
   fmt.Println("The final string obtained by concatenating the above strings together is:\n", result)
}

Output

The first string is:
 Life is what happens	

The second string is:
 when you're busy making other plans.

The final string obtained by concatenating the above strings together is:
 Life is what happens	when you're busy making other plans.

Conclusion

We have successfully compiled and executed a go language program to demonstrate the string concatenation along with examples. Here we have used both the external functions and library functions in go language in order to obtain the result.

Updated on: 13-Feb-2023

111 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements