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?
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.
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.
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.
A string in Go is a value. Thus, a string cannot be nil .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With