How to convert a golang array to string?
In Go language, we can convert an array of strings into a single string using the Join function in the strings package.
Here is a sample code:
package main
import (
"fmt"
"strings"
)
func main() {
arr := []string{"hello", "world", "!"}
// 使用Join函数将数组转换为字符串,使用空格分隔
str := strings.Join(arr, " ")
fmt.Println(str) // 输出: hello world !
}
In the above code, we first define a string array arr, then we use the strings.Join function to convert the array into a single string str, with the second parameter being a string used to separate the array elements, in this case a space. Finally, we print out the converted string using fmt.Println.
If you run the code above, you will get the output result of “hello world!”.