Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types in Go
  • Go Keywords
  • Go Control Flow
  • Go Functions
  • GoLang Structures
  • GoLang Arrays
  • GoLang Strings
  • GoLang Pointers
  • GoLang Interface
  • GoLang Concurrency
Open In App
Next Article:
Different ways to concatenate two strings in Golang
Next article icon

Different ways to concatenate two strings in Golang

Last Updated : 28 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Go, strings are immutable sequences of bytes encoded with UTF-8. Concatenating two or more strings into a single string is straightforward in Go, and there are several ways to accomplish it. In this article,we will learn various ways to concatenate two strings in Golang.

Example

Input:
s1 := "Hello, "
s2 := "Geeks!"

Output:
"Hello,Geeks!"

Syntax

s1 + s2                                             #Using the "+" Operator

var b bytes.Buffer
b.WriteString(s1) # Using bytes.Buffer and WriteString()
b.WriteString(s2)

fmt.Sprintf("%s%s", s1, s2) # Using fmt.Sprintf

s1 += s2 # Using the += Operator (String Append)

strings.Join([]string{s1, s2}, "") # Using strings.Join

var builder strings.Builder
builder.WriteString(s1) # Using strings.Builder and WriteString()
builder.WriteString(s2)

Table of Content

  • Using the "+" Operator
  • Using bytes.Buffer and WriteString()
  • Using fmt.Sprintf
  • Using the += Operator (String Append)
  • Using strings.Join
  • Using strings.Builder and WriteString()

Using the "+" Operator

The + operator is the simplest way to concatenate strings in Go. This operator combines two or more strings.

Syntax

s1 + s2

Example:

Go
package main import "fmt"  func main() {     s1 := "Hello, "     s2 := "Geeks!"      // Concatenating using + operator     result := s1 + s2     fmt.Println("", result) } 

Output
 Hello, Geeks! 

Using bytes.Buffer and WriteString()

The bytes.Buffer approach allows efficient string concatenation without generating intermediate strings, using WriteString() to append each segment.

Syntax

var b bytes.Buffer
b.WriteString(s1)
b.WriteString(s2)

Example:

Go
package main import (     "bytes"     "fmt" )  func main() {     s1 := "Hello, "     s2 := "Geeks!"      // Initializing a bytes buffer     var b bytes.Buffer     b.WriteString(s1)     b.WriteString(s2)     fmt.Println("", b.String()) } 

Output
 Hello, Geeks! 

Using fmt.Sprintf

The fmt.Sprintf function offers a formatted approach to string concatenation.

Syntax:

fmt.Sprintf("%s%s", s1, s2)

Example:

Go
package main import "fmt"  func main() {     s1 := "Hello, "     s2 := "Geeks!"      // Concatenating using fmt.Sprintf     result := fmt.Sprintf("%s%s", s1, s2)     fmt.Println("", result) } 

Output
 Hello, Geeks! 

Using the += Operator (String Append)

In Go, you can append to an existing string using the += operator. This operation adds the second string to the end of the first.

Syntax

s1 += s2

Example

Go
package main import "fmt"  func main() {     s1 := "Hello, "     s2 := "Geeks!"      // Concatenating using += operator     s1 += s2     fmt.Println("", s1) } 

Output
 Hello, Geeks! 

Using strings.Join

The strings.Join function can concatenate elements from a slice of strings with a specified separator. While it is most useful for multiple strings, it works well for pairs as well.

Syntax

strings.Join([]string{s1, s2}, "")

Example:

Go
package main import (     "fmt"     "strings" )  func main() {     s1 := "Hello, "     s2 := "Geeks!"      // Concatenating using strings.Join     result := strings.Join([]string{s1, s2}, "")     fmt.Println("", result) } 

Output
 Hello, Geeks! 

Using strings.Builder and WriteString()

The strings.Builder type provides a similar approach to bytes.Buffer for efficient string concatenation with WriteString().

Syntax

var builder strings.Builder 
builder.WriteString(s1)
builder.WriteString(s2)

Example

Go
package main import (     "fmt"     "strings" )  func main() {     s1 := "Hello, "     s2 := "Geeks!"      // Initializing a strings builder     var builder strings.Builder     builder.WriteString(s1)     builder.WriteString(s2)     fmt.Println("", builder.String()) } 

Note:

The error undefined: strings.Builder will occur if your Go version is earlier than 1.10 because the strings.Builder type was introduced in Go 1.10. If your Go environment is set to a version earlier than 1.10, strings.Builder will be undefined. To resolve this issue, make sure that your Go version is 1.10 or higher.


Next Article
Different ways to concatenate two strings in Golang

A

ankita_saini
Improve
Article Tags :
  • Go Language
  • Golang
  • Golang-String

Similar Reads

    Different ways to compare Strings in Golang
    In Go, strings are immutable sequences of bytes encoded in UTF-8. You can compare them using comparison operators or the strings.Compare function. In this article,we will learn different ways to compare Strings in Golang.Examplepackage main import ( "fmt" "strings" ) func main() { s1 := "Hello" s2 :
    2 min read
    Different Ways to Convert the Boolean Type in String in Golang
    In order to convert Boolean Type to String type in Golang , you can use the strconv and fmt package function. 1. strconv.FormatBool() Method: The FormatBool is used to Boolean Type to String. It returns "true" or "false" according to the value of b. Syntax: func FormatBool(b bool) string Example : C
    2 min read
    Different Ways to Convert an Integer Variable to String in Golang
    Integer variable cannot be directly convert into String variable. In order to convert string to integer type in Golang , you have store value of integer variable as string in string variable. For this, we are using strconv and fmt package functions. 1. Itoa() Function:  The Itoa stands for Integer t
    3 min read
    How to Convert string to integer type in Golang?
    Strings in Golang is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go language, both signed and unsigned integers are available in four different sizes. In order to convert string to integer type in Golang, you can
    2 min read
    time.String() Function in Golang With Examples
    In Go language, time packages supplies functionality for determining as well as viewing time. The String() function in Go language is used to find a string that represents the duration formats as "24h6m0.7s". Here, the foremost zero units are deleted. And the stated duration with less than one-secon
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences