
- Go - Home
- Go - Overview
- Go - Environment Setup
- Go - Program Structure
- Go - Basic Syntax
- Go - Data Types
- Go - Variables
- Go - Constants
- Go - Identifiers
- Go - Keywords
- Go - Operators
- Go - Arithmetic Operators
- Go - Assignment Operators
- Go - Relational Operators
- Go - Logical Operators
- Go - Bitwise Operators
- Go - Miscellaneous Operators
- Go - Operators Precedence
- Go Decision Making
- Go - Decision Making
- Go - If Statement
- Go - If Else Statement
- Go - Nested If Statements
- Go - Switch Statement
- Go - Select Statement
- Go Control Flow Statements
- Go - For Loop
- Go - Nested for Loops
- Go - Break Statement
- Go - Continue Statement
- Go - Goto Statement
- Go Functions
- Go - Functions
- Go - Call by Value
- Go - Call by Reference
- Go - Functions as Values
- Go - Function Closure
- Go - Function Method
- Go - Anonymous function
- Go Strings
- Go - Strings
- Go - String Length
- Go - String Concatenation
- Go - Compare Strings
- Go - Split String
- Go - Substring Extraction
- Go - String Replacement
- Go - String Interpolation
- Go - Parse Date Strings
- Go Arrays
- Go - Arrays
- Go - Multidimensional Arrays
- Go - Multidimensional Arrays
- Go - Passing Arrays to Functions
- Go - Pointers
- Go - Pointers
- Go - Array of pointers
- Go - Pointer to pointer
- Go - Passing pointers to functions
- Go Advanced Control Structures
- Go - Scope Rules
- Go - Dereferencing Pointer
- Go - Structures
- Go - Slice
- Go - Slice of Slices
- Go - Range
- Go - Maps
- Go - Recursion
- Go - Type Casting
- Go - Interfaces
- Go - Type Assertion
- Go - Error Handling
- Go - Concurrency
- Go - Regular Expression
- Go - Inheritance
- Go - Packages
- Go - Templates
- Go - Reflection
- Go - Generics
- Go File Handling
- Go - Read File By Word
- Go - Read File By Line
- Go - Read CSV Files
- Go - Delete File
- Go - Rename & Move File
- Go - Truncate a File
- Go - File Read-Write Mode W/O Truncation
- Go Miscellaneous
- Go - defer Keyword
- Go - Fmt Package
- Go - Zero Value
- Go - Import
Go - String Concatenation
In Golang, string concatenation involves joining two or more strings together to form a single string. we can concatenate the strings using '+' operator and strings.Join() function.
Let us go through the example to have a clear idea about the string concatenation −
String Concatenation Using + (Plus) Operator
You can use the plus (+) operator to combine two or more strings.
Example
In this example, we initialize two strings, 'str1' and 'str2', and then concatenate them using the '+' operator and print the combined result.
package main import ( "fmt" ) func main() { str1 := "Hello..." str2 := "How are you doing?" fmt.Println("Concatenated String:", str1 + str2) }
Output
It will produce the following output
Concatenated String: Hello...How are you doing?
String Concatenation Using strings.Join() Function
The strings.Join() is a built-in function in Golang which is used to concatenate a slice of strings into a single string.
Syntax
Its syntax is as follows −
func Join(stringSlice []string, sep string) string
Where,
- stringSlice – The strings to be concatenated.
- sep – It is a separator string that is to be placed between the slice elements.
Example 1
In this example, we initialize two slices of strings, 'm' and 'n'. then uses the strings.Join() function to concatenate elements of each slice with different delimiters ('-', '/', '*', '$') and print the joined strings as the result.
package main import ( "fmt" "strings" ) func main() { // Initializing the Strings m := []string{"IndexByte", "String", "Function"} n := []string{"Golang", "IndexByte", "String", "Package"} // Display the Strings fmt.Println("Set 1 - Slices of Strings:", m) fmt.Println("Set 2 - Slices of Strings:", n) // Using the Join Function output1 := strings.Join(m, "-") output2 := strings.Join(m, "/") output3 := strings.Join(n, "*") output4 := strings.Join(n, "$") // Display the Join Output fmt.Println("\n Joining the slices of Set 1 with '-' delimiter: \n", output1) fmt.Println("\n Joining the slices of Set 1 with '/' delimiter: \n", output2) fmt.Println("\n Joining the slices of Set 2 with '*' delimiter: \n", output3) fmt.Println("\n Joining the slices of Set 2 with '$' delimiter: \n", output4) }
Output
It will generate the following output −
Set 1 - Slices of Strings: [IndexByte String Function] Set 2 - Slices of Strings: [Golang IndexByte String Package] Joining the slices of Set 1 with '-' delimiter: IndexByte-String-Function Joining the slices of Set 1 with '/' delimiter: IndexByte/String/Function Joining the slices of Set 2 with '*' delimiter: Golang*IndexByte*String*Package Joining the slices of Set 2 with '$' delimiter: Golang$IndexByte$String$Package
Example 2
In this example, we initialize a slice of strings s and two delimiters substr and substr1. It uses the strings.Join() function to concatenate the elements of the slice with the specified delimiters and prints the results.
package main import ( "fmt" "strings" ) func main() { // Defining the Variables var s []string var substr string var substr1 string var result string var output string // Intializing the Strings s = []string{"This", "is", "String", "Function"} substr = "..." substr1 = " " // Display the input slice of strings fmt.Println("Input Slice of Strings:", s) // Using the Join Function result = strings.Join(s, substr) output = strings.Join(s, substr1) // Displaying output of Join function fmt.Println("Joining with '...' delimiter:", result) fmt.Println("Joining with ' ' delimiter:", output) }
Output
It will generate the following output −
Input Slice of Strings: [This is String Function] Joining with '...' delimiter: This...is...String...Function Joining with ' ' delimiter: This is String Function