Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang convert "type []string" to string

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?

like image 715
user3888307 Avatar asked Jan 20 '17 05:01

user3888307


People also ask

How do you convert interface to string in Golang?

Use fmt. Sprintf to convert an interface value to a string. In fact, the same technique can be used to get a string representation of any data structure.

How do I cast to string in Go?

You can convert numbers to strings by using the strconv. Itoa method from the strconv package in the Go standard libary. If you pass either a number or a variable into the parentheses of the method, that numeric value will be converted into a string value.

Which method is used to convert a string into an array?

Using toArray() Method The toArray() function of the List class can also be used to convert a string to array in Java. It takes a list of type String as the input and converts each entity into an element of a string array.

Can a string be nil in Golang?

A string in Go is a value. Thus, a string cannot be nil .


1 Answers

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

like image 162
Tom Regner Avatar answered Sep 25 '22 11:09

Tom Regner