C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Strings.Join: We import the "strings" module. With strings.Join, we combine all elements of a string slice into a string.
Tip: The second argument to strings.Join is the delimiter. For no delimiter, please use an empty string literal.
Golang program that converts string slice to string
package main
import (
"fmt"
"strings"
)
func main() {
values := []string{"one", "two", "three"}
// Convert string slice to string.
// ... Has comma in between strings.
result1 := strings.Join(values, ",")
fmt.Println(result1)
// ... Use no separator.
result2 := strings.Join(values, "")
fmt.Println(result2)
}
Output
one,two,three
onetwothree
Strconv.Itoa: This converts an int into a string. We then place these strings (text) into the valuesText slice.
Finally: We convert our string slice into a string with the strings.Join method.
Golang program that converts int slice to string
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
// The int slice we are converting to a string.
values := []int{10, 200, 3000}
valuesText := []string{}
// Create a string slice using strconv.Itoa.
// ... Append strings to it.
for i := range values {
number := values[i]
text := strconv.Itoa(number)
valuesText = append(valuesText, text)
}
// Join our string slice.
result := strings.Join(valuesText, "+")
fmt.Println(result)
}
Output
10+200+3000