English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In Go language, strings are used with UTF-8An immutable sequence of arbitrary bytes. In Go strings, the process of adding two or more strings to a new single string is called concatenation. The simplest way to concatenate two or more strings in Go language is to use the operator (+). Also known as the concatenation operator.
//Concatenating strings package main import "fmt" func main() { //Create and initialize a string //Using var keyword var str1 string str1 = "Welcome!" var str2 string str2 = "oldtoolbag.com" //Concatenating strings //Using+operator fmt.Println("New string 1: ", str1+str2) //Create and initialize a string //Using shorthand declaration str3 := "Geeks" str4 := "Geeks" //Concatenating strings //Using+operator result := str3 + "for" + str4 fmt.Println("New string 2: ", result) }
Output:
New string 1: Welcome!oldtoolbag.com New string 2: GeeksforGeeks
Using bytes.Buffer:You can also concatenate bytes of strings to create a string by using bytes.Buffer and WriteString() method. It is defined under the bytes package. It can prevent the generation of unnecessary string objects, which means it will not generate a new string from two or more strings (such as+operator).
//Using bytes.Buffer in the WriteString() function package main import ( "bytes" "fmt" ) func main() { //Create and initialize a string //Using bytes.Buffer with // WriteString() function var b bytes.Buffer b.WriteString("G") b.WriteString("e") b.WriteString("e") b.WriteString("k") b.WriteString("s") fmt.Println("String: ", b.String()) b.WriteString("f") b.WriteString("o") b.WriteString("r") b.WriteString("G") b.WriteString("e") b.WriteString("e") b.WriteString("k") b.WriteString("s") fmt.Println("String: ", b.String()) }
Output:
String: Geeks String: w3codebox
Using Sprintf:In Go language, you can also useSprintf()method to concatenate strings.
//Using Sprintf function package main import "fmt" func main() { //Create and initialize a string str1 := "Tutorial" str2 := "of" str3 := "Go" str4 := "Language" //Using string concatenation // Sprintf() function result := fmt.Sprintf("%s%s%s%s", str1, str2, str3, str4) fmt.Println(result) }
Output:
TutorialofGoLanguage
Using+ = operator or string concatenation:In Go strings, you can use+ = operator concatenationThe = operator adds new or given strings to the end of the specified string.
// Using += operator concatenates strings package main import "fmt" func main() { //Create and initialize a string str1 := "Welcome" str2 := "w3codebox" //Using += operator str1 += str2 fmt.Println("String: ", str1) str1 += "This is the tutorial of Go language" fmt.Println("String: ", str1) str2 += "Portal" fmt.Println("String: ", str2) }
Output:
String: Welcomew3codebox String: Welcomew3codeboxThis is the tutorial of Go language String: w3codeboxPortal
Using Join() function:This function concatenates all existing elements in the string slice into a single string. This function is available in the string package.
Syntax:
func Join(str []string, sep string) string
Here,stris a string that can be used to concatenate elements, sep is the separator placed between elements in the final string.
//to concatenate all elements //appears in the string segment package main import ( "fmt" "strings" ) func main() { //Create and initialize a string slice myslice := []string{"Welcome", "To", "w3codebox", "Portal" //to concatenate elements //to represent in the slice //Using the join() function result := strings.Join(myslice, ""-") fmt.Println(result) }
Output:
Welcome-To-w3codebox-Portal