Golang convert "type [] string" to string

I am sure this is a simple question, but I stumble upon this all the time. I see others as well.

I see that some people create a for loop and go through a slice to create a string, is there an easier way to convert []string to string ?

Will sprintf do this?

+7
string types string-concatenation go
source share
2 answers

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

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

+12
source share

This is a simple example that you can insert into the main function:

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

And a link to a working example on the playground.

Or using a very simple function a simple function

+5
source share

All Articles