In Go language, the string is an immutable chain of arbitrary bytes encoded with UTF-8 encoding. In Go strings, the process of adding two or more strings into a new single string is known as concatenation. The simplest way of concatenating two or more strings in the Go language is by using + operator. It is also known as a concatenation operator.
Example:
// Go program to illustrate // how to concatenate strings package main import "fmt" func main() { // Creating and initializing strings // using var keyword var str1 string str1 = "Welcome!" var str2 string str2 = "GeeksforGeeks" // Concatenating strings // Using + operator fmt.Println("New string 1: ", str1+str2) // Creating and initializing strings // 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!GeeksforGeeks New string 2: GeeksforGeeks
Other Methods for concatenating strings
- Using bytes.Buffer: You can also create a string by concatenating the bytes of the strings using
bytes.Bufferwith WriteString() method. It is defined under bytes package. It prevents the generation of the unnecessary string object, means it doesn’t generate a new string like in + operator from two or more strings.Example:
// Go program to illustrate how to concatenate strings// Using bytes.Buffer with WriteString() functionpackage mainimport ("bytes""fmt")func main() {// Creating and initializing strings// Using bytes.Buffer with// WriteString() functionvar b bytes.Bufferb.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())}chevron_rightfilter_noneOutput:
String: Geeks String: GeeksforGeeks
- Using Sprintf: In Go language, you can also concatenate string using Sprintf() method.
Example:
// Go program to illustrate how to concatenate strings// Using Sprintf functionpackage mainimport"fmt"func main() {// Creating and initializing stringsstr1 :="Tutorial"str2 :="of"str3 :="Go"str4 :="Language"// Concatenating strings using// Sprintf() functionresult := fmt.Sprintf("%s%s%s%s", str1,str2, str3, str4)fmt.Println(result)}chevron_rightfilter_noneOutput:
TutorialofGoLanguage
- Using += operator or String append: In Go strings, you are allowed to append a string using += operator. This operator adds a new or given string to the end of the specified string.
Example:
// Go program to illustrate how// to concatenate strings// Using += operatorpackage mainimport"fmt"func main() {// Creating and initializing stringsstr1 :="Welcome"str2 :="GeeksforGeeks"// Using += operatorstr1 += str2fmt.Println("String: ", str1)str1 +="This is the tutorial of Go language"fmt.Println("String: ", str1)str2 +="Portal"fmt.Println("String: ", str2)}chevron_rightfilter_noneOutput:
String: WelcomeGeeksforGeeks String: WelcomeGeeksforGeeksThis is the tutorial of Go language String: GeeksforGeeksPortal
- Using Join() function: This function concatenates all the elements present in the slice of string into a single string. This function is available in string package.
Syntax:
func Join(str []string, sep string) string
Here, str is the string from which we can concatenate elements and sep is the separator which is placed between the elements in the final string.
Example:
// Go program to illustrate how to// concatenate all the elements// present in the slice of the stringpackage mainimport ("fmt""strings")func main() {// Creating and initializing slice of stringmyslice := []string{"Welcome","To","GeeksforGeeks","Portal"}// Concatenating the elements// present in the slice// Using join() functionresult := strings.Join(myslice,"-")fmt.Println(result)}chevron_rightfilter_noneOutput:
Welcome-To-GeeksforGeeks-Portal
Recommended Posts:
- Different ways to compare Strings in Golang
- Different Ways to Find the Type of an Object in Golang
- Different Ways to Convert the Boolean Type in String in Golang
- Different Ways to Convert an Integer Variable to String in Golang
- Different Ways to Find the Type of Variable in Golang
- How to compare Structs with the Different Values Assigned to Data Fields in Golang?
- Different Types of Recursion in Golang
- Strings in Golang
- How to Sort a Slice of Strings in Golang?
- strings.ContainsRune() Function in Golang with Examples
- strings.Compare() Function in Golang with Examples
- strings.Index() Function in Golang With Examples
- strings.LastIndexAny() Function in Golang With Examples
- strings.LastIndex() Function in Golang With Examples
- strings.SplitN() Function in Golang with Examples
- strings.Fields() Function in Golang With Examples
- strings.Title() Function in Golang With Examples
- strings.FieldsFunc() Function in Golang With Examples
- strings.Map() Function in Golang With Examples
- strings.Replace() Function in Golang With Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

