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.


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 use Join().


In the example below, "Hello" and "World" are combined into one string with the Join() function.

package main
import (
func main() {
strs := []string{"Hello", "World"}
result := strings.Join(strs, ", ")

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.


All contributors

Looking to contribute?

Learn Go on Codecademy