Open In App
Related Articles

strings.Join() Function in Golang With Examples

Improve Article
Improve
Save Article
Save
Like Article
Like

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:

func Join(s []string, sep string) string

Here, 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, "-"))
}


Output:

Geeks-For-Geeks

Example 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, " "))
}


Output:

A Computer-science portal for Geeks

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 10 May, 2020
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials