strings.Join() Function in Golang With Examples
Last Updated :
10 May, 2020
Improve
strings.Join() Function in Golang concatenates all the elements present in the slice of string into a single string. This function is available in the string package.
Syntax:
C
Output:
C
Output:
func Join(s []string, sep string) stringHere, s is the string from which we can concatenate elements and sep is the separator which is placed between the elements in the final string. Return Value: It returns a string. Example 1:
// Golang program to illustrate the
// use of strings.Join Function
package main
// importing fmt and strings
import (
"fmt"
"strings"
)
// calling main method
func main() {
// array of strings.
str := []string{"Geeks", "For", "Geeks"}
// joining the string by separator
fmt.Println(strings.Join(str, "-"))
}
Geeks-For-GeeksExample 2:
// Golang program to illustrate the
// use of strings.Join Function
package main
// importing fmt and strings
import (
"fmt"
"strings"
)
// calling main method
func main() {
// array of strings.
str := []string{"A", "Computer-science", "portal", "for", "Geeks"}
// joining the string by separator in middle.
fmt.Println(strings.Join(str, " "))
}
A Computer-science portal for Geeks