Go Join()

daniM.0064557070's avatar
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.

  • Back-end developers deal with the hidden processes that run behind the scenes, building APIs and databases that power the front-end.
    • Includes 41 Courses
    • With Professional Certification
    • Beginner Friendly.
      105 hours
  • Learn how to use Go (Golang), an open-source programming language supported by Google!
    • Beginner Friendly.
      6 hours

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

Example

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

package main
import (
"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.

Code
Output

All contributors

Contribute to Docs

Learn Go on Codecademy

  • Back-end developers deal with the hidden processes that run behind the scenes, building APIs and databases that power the front-end.
    • Includes 41 Courses
    • With Professional Certification
    • Beginner Friendly.
      105 hours
  • Learn how to use Go (Golang), an open-source programming language supported by Google!
    • Beginner Friendly.
      6 hours