Join()
Published Jul 6, 2023
Contribute to Docs
The Join()
function takes a given separator to concatenate the elements of a slice of strings into a single string.
Syntax
strings.Join(a []string, sep string) string
a
: The segments of strings to be joined.
sep
: The separator between the elements in the final string.
The function outputs a single string that is the concatenation of the strings in slice a
that are separated by the separator sep
.
Note: The
strings
package must be called to useJoin()
.
Example
In the example below, "Hello"
and "World"
are combined into one string with the Join()
function.
package mainimport ("fmt""strings")func main() {strs := []string{"Hello", "World"}result := strings.Join(strs, ", ")fmt.Println(result)}
This example results in the following output:
Hello, World
Codebyte Example
In the code below, several strings are concatenated into a single string, separated by spaces, using the Join()
function.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Go on Codecademy
- Career path
Back-End Engineer
Back-end developers deal with the hidden processes that run behind the scenes, building APIs and databases that power the front-end.Includes 41 CoursesWith Professional CertificationBeginner Friendly105 hours - Free course
Learn Go
Learn how to use Go (Golang), an open-source programming language supported by Google!Beginner Friendly6 hours