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

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.

I'm working on some more Cheat Sheets book on Jquery, TypeScript, React js and for other languages. I will send it once it's completed.

Stay tuned working on React Js Cheat Sheets Book

Related Posts