golang convert "type []string" to string

this is simple example, which You can paste to main function:

  stringArray := []string {"Hello","world","!"}
  justString := strings.Join(stringArray," ")
  fmt.Println(justString)

And link to working example on playground.

Or using very simple function simple function


It can be done easily using Join function by importing strings package. You need to pass the slice of strings and the separator you need to separate the elements in the string. (examples: space or comma)

func Join(elems []string, sep string) string

Example Code :

package main

import (
    "fmt"
    "strings"
)

func main() {
    sliceStr := []string{"a","b","c","d"}
    str := strings.Join(sliceStr,", ")
    fmt.Println(str)
}

//output: a, b, c, d

You can use strings.Join(arr []string, separator string) string, as in pretty much any other language I know

https://golang.org/pkg/strings/#Join


Will Sprint do it?

Yes indeed!

Here is another way to convert to a string if all you care about is that it is a string and not specifically how it looks (see answers above with strings.Join for a little more flexibility).

The advantage of this method (or variations such as Sprintf), is it will work with (almost) every other data such as maps and structs and any custom type that implements the fmt.Stringer inteface.

  stringArray := []string {"Hello","world","!"}
  justString := fmt.Sprint(stringArray)

Here is a link to a working example.