How to convert list to string in Golang?
December 09, 2022Hi Friends 👋,
Welcome To aGuideHub! ❤️
In Go, you can convert a list (slice) of strings to a single string by using the strings.Join()
function. This function takes a slice of strings and a separator string, and returns a single string that consists of the elements of the slice joined together with the separator string between each element.
Here is an example of how to convert a list of strings to a single string in Go:
package main
import (
"fmt"
"strings"
)
func main() {
list := []string{"apple", "banana", "cherry"}
s := strings.Join(list, ", ")
fmt.Println(s)
}
In this example, we create a list of strings and then use the strings.Join()
function to convert it to a single string. The resulting string will be “apple, banana, cherry”, with a comma and a space between each element of the list.
You can use this technique to convert any list of strings to a single string in Go.
Output:
apple, banana, cherry
All the best 👍