join an array of substrings into a string in go

To join an array of substrings into a single string in Go, you can use the strings.Join() function. strings.Join() takes two arguments: the first argument is the slice of strings you wish to join, and the second argument is the separator character you'd like to use.

Here's an example showing how to join an array of substrings into a single string in Go:

main.go
package main

import (
    "fmt"
    "strings"
)

func main() {
    // create a slice of strings
    slice := []string{"This", "is", "a", "slice", "of", "strings"}

    // join the strings using a space as the separator
    str := strings.Join(slice, " ")

    // output the resulting string
    fmt.Println(str)
}
315 chars
18 lines

Output:

main.go
This is a slice of strings
27 chars
2 lines

related categories

gistlibby LogSnag