+ number splicing
This kind of splicing is the simplest and easiest for us to use, because it is not limited to programming languages, such as Go language, Java, and they are + operators, which are calculated at runtime.
var s string s+="Nickname?"+":"+"Zhiqiang 1224"+"\n" s+="Contact information QQ"+":"+"354662600"+"\n" fmt.Println(s)
fmt splicing
This kind of splicing is carried out with the help of fmt.Sprint series functions, and then the spliced string is returned.
fmt.Sprint("Nickname?",":","Zhiqiang 1224","\n","Contact information QQ",":","354662600")
Join splicing
This is to use the strings.Join function to splice, accept a string array, and convert it into a spliced string.
s:=[]string{"Nickname?",":","Zhiqiang 1224","\n","Contact information QQ",":","354662600"} fmt.Println(strings.Join(s,""))
buffer splicing
It uses the string splicing by bytes.Buffer, which is a very flexible structure. It can not only splice strings, but also byte, run, etc. it also implements the io.Writer interface and is very convenient to write.
var b bytes.Buffer b.WriteString("Nickname?") b.WriteString(":") b.WriteString("Zhiqiang 1224") b.WriteString("\n") b.WriteString("Contact information QQ") b.WriteString(":") b.WriteString("354662600") b.WriteString("\n") fmt.Println(b.String())
builder splicing
In order to improve the performance of buffer splicing, a builder type was added from go 1.10 to improve the performance of string splicing. It uses almost the same as buffer.
var b strings.Builder b.WriteString("Nickname?") b.WriteString(":") b.WriteString("Zhiqiang 1224") b.WriteString("\n") b.WriteString("Contact information QQ") b.WriteString(":") b.WriteString("354662600") b.WriteString("\n") fmt.Println(b.String())
Splicing function transformation
func StringPlus(p []string) string{ var s string l:=len(p) for i:=0;i<l;i++{ s+=p[i] } return s } func StringFmt(p []interface{}) string{ return fmt.Sprint(p...) } func StringJoin(p []string) string{ return strings.Join(p,"") } func StringBuffer(p []string) string { var b bytes.Buffer l:=len(p) for i:=0;i<l;i++{ b.WriteString(p[i]) } return b.String() } func StringBuilder(p []string) string { var b strings.Builder l:=len(p) for i:=0;i<l;i++{ b.WriteString(p[i]) } return b.String() }