How to convert list to string in Golang?

Hi 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

Try it yourself

All the best 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts