Golang String Concatenation

Strings are a fundamental building block of programming. String concatenation means add strings together.

String Concatenation Using + (Concatenation Operator)
// Golang program to concatenate strings
package main

import "fmt"

func main() {
	// Creating and initializing strings
	// using var keyword
	var str1 string
	str1 = "Great"

	var str2 string
	str2 = "Britain"

	// Concatenating strings
	// Using + operator
	fmt.Println(str1 + str2)

	// Creating and initializing strings
	// Using shorthand declaration
	str3 := "New"
	str4 := "York"

	// Concatenating strings
	// Using + operator
	result := str3 + " " + str4

	fmt.Println(result)
}

This Golang program demonstrates how to concatenate strings. It first initializes two strings str1 with the value "Great" and str2 with the value "Britain" using the var keyword.

These strings are then concatenated using the + operator and the result "GreatBritain" is printed to the console.

The program then uses a shorthand declaration to initialize another two strings str3 with the value "New" and str4 with the value "York". These strings are concatenated with a space in between, resulting in "New York", which is then printed to the console.


Append String Using += operator
// Golang program to illustrate
// how to append string
package main

import "fmt"

func main() {

	// Creating and initializing strings
	str1 := "New"
	str2 := "York"

	// Using += operator
	str1 += str2
	fmt.Println(str1)

	str1 += "London"
	fmt.Println(str1)
}

The Golang program demonstrates how to append strings. It initializes two strings, str1 with the value "New" and str2 with the value "York".

The program then appends str2 to str1 using the += operator and prints the result, which is "NewYork".

After that, the program appends the string "London" to the modified str1 and prints the resulting string, which is "NewYorkLondon".


String Concatenation Using Strings Builder
// Golang program to illustrate
// how to concatenate strings
// Using strings.Builder with WriteString() function
package main

import (
	"fmt"
	"strings"
)

func main() {
	// Creating and initializing strings
	// Using strings.Builder with
	// WriteString() function
	var sb strings.Builder

	sb.WriteString("London")
	sb.WriteString("-")
	sb.WriteString("5214")

	fmt.Println(sb.String())
}

This Golang program demonstrates string concatenation using the strings.Builder type and its WriteString() method.

The program initializes a strings.Builder variable named sb and then appends the string "London", followed by a dash "-", and then the string "5214" to it.

The concatenated result "London-5214" is then printed to the standard output using the fmt.Println() function.


String Concatenation Using Bytes Buffer
// Golang program to illustrate
// how to concatenate strings
// Using bytes.Buffer with WriteString() function
package main

import (
	"bytes"
	"fmt"
)

func main() {
	// Creating and initializing strings
	// Using bytes.Buffer with
	// WriteString() function
	var b bytes.Buffer

	b.WriteString("London")
	b.WriteString("#")
	b.WriteString("5214")

	fmt.Println(b.String())
}

The given Golang program demonstrates how to concatenate strings using the bytes.Buffer type and its WriteString() method.

The program uses the bytes.Buffer type to concatenate three strings: "London", "#", and "5214".

It initializes an empty buffer b and then appends each of these strings to the buffer using the WriteString() method.

Finally, the concatenated result "London#5214" is printed to the console using the fmt.Println() function.


String Concatenation Using Sprintf Method
// Golang program to illustrate
// how to concatenate strings
// Using Sprintf() method
package main

import "fmt"

func main() {
	// Creating and initializing strings
	s1 := "London"
	s2 := "@"
	s3 := "5421"

	result := fmt.Sprintf("%s%s%s", s1, s2, s3)

	fmt.Println(result)
}

This Golang program demonstrates how to concatenate multiple strings using the Sprintf() method from the "fmt" package.

The program initializes three strings: s1 with the value "London", s2 with the value "@", and s3 with the value "5421". It then concatenates these strings using fmt.Sprintf() and stores the result in the variable result.

Finally, the program prints the concatenated result, which will be "London@5421".


Repeat same String Multiple Times
// Golang program to illustrate
// how to repeat same string multiple times
// using Sprintf() method
package main

import "fmt"
import "strings"

func main() {
	// Creating and initializing strings
	s1 := "London"
	r := fmt.Sprintf("%s", strings.Repeat(s1, 3))	
	fmt.Println(r)
}

This Golang program demonstrates how to repeat a string multiple times using the Sprintf() method from the fmt package and the Repeat() function from the strings package.

The program defines a string s1 with the value "London". It then repeats the string three times using the strings.Repeat() function. The repeated string is then formatted using fmt.Sprintf() and assigned to the variable r.

Finally, the program prints the repeated string (which would be "LondonLondonLondon") to the console.



Most Helpful This Week