Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

golang convert "type []string" to string

Writer Matthew Harrington

I'm sure this is a simple question, but I keep bumping into this. I see others are as well.

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

Will sprintf do it?

2

6 Answers

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

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 functionsimple function

1

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.

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

If you don't care about the separator, you can use path:

package main
import "path"
func main() { a := []string{"south", "north"} s := path.Join(a...) println(s == "south/north")
}
package main
import (
"fmt"
"reflect"
"strings"
)
func main() {
str1 := []string{"Trump", "In", "India", "On", "Feb 25"}
fmt.Println(str1)
fmt.Println(reflect.TypeOf(str1))
str2 := strings.Join(str1, " ")
fmt.Println(str2)
fmt.Println(reflect.TypeOf(str2))
str3 := strings.Join(str1, ", ")
fmt.Println(str3)
fmt.Println(reflect.TypeOf(str3))
}

Below is the ouput of the above program :-

go run hello.go
[Trump In India On Feb 25]
[]string
Trump In India On Feb 25
string
Trump, In, India, On, Feb 25
string

In the above code, first, we have defined a slice of string and then use the reflect package to determine the datatype of the slice. We have imported the “strings” module. With strings.Join() method, and we combine all elements of a string slice into a string. So, Golang string.Join() function that converts slice to string. We have passed the space(” “) as a delimiter. So we will join the slice elements by space.

The second argument to strings.Join() is the delimiter. For no delimiter, please use an empty string literal.

In the next step, we have again used the TypeOf() function to check the data type.

Then we have used the Golang string.Join() function again, but this time, we have passed (,) Comma. So, command separated values will be returned, which is also a type of string.

So, if you want to get CSV values in Golang, then you can use the Go string.Join() method.

You can also try with functions:-

// abc.go
package main
type deck []string
func (cards deck) toString() string { // converts slice to string return strings.Join([]string(cards), ",")
}
//main.go
package main
import "fmt"
func main() { cards := []string {"Trump", "In", "India", "On", "Feb 25"} fmt.Println(cards.toString())
}

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy