1.字符串拼接
一般对于少量的字符串拼接可以直接用+来连接,不过最好的方法还是Builder。
buffer.Builder
package main
import (
"fmt"
)
func main() {
s1 := "哈哈"
s2 := "嘻嘻"
var build strings.Builder
build.WriteString(s1)
build.WriteString(s2)
s3 := build.String()
fmt.Println(s3)
}
输出:
哈哈嘻嘻
2.golang字符串数组声明与初始化
//法一
var words []string
words = []string{"我","爱","你"}
//法二
words := []string{"我","爱","你"}
3.字符串数组转字符串
strings.Join(elems []string, sep string)
package main
import (
"fmt"
"strings"
)
func main() {
var words []string
words = []string{"大理", "苏州", "西安"}
fmt.Println(words)
res := strings.Join(words, ",")
fmt.Println(res)
fmt.Println(reflect.TypeOf(res))
}
输出:
大理,苏州,西安
string