SoFunction
Updated on 2025-03-05

Implementation of efficient stitching of Go strings

+ sign splicing

This kind of splicing is the simplest and easiest to use by us because it is unlimited in programming languages, such as Go and Java. They are + operators, calculated at runtime.

var s string
s+="Nick name"+":"+"Zhiqiang 1224"+"\n"
s+="Contact QQ"+":"+"354662600"+"\n"
(s)

fmt splicing

This stitching is spliced ​​with the help of series functions, and then the spliced ​​string is returned.

("Nick name",":","Zhiqiang 1224","\n","Contact QQ",":","354662600")

Join splicing

This is a function that uses splicing, accepts an array of strings, and converts it into a spliced ​​string.

s:=[]string{"Nick name",":","Zhiqiang 1224","\n","Contact QQ",":","354662600"}
((s,""))

Buffer splicing

It uses string splicing. It is a very flexible structure. It can not only splice strings, but also byte, rune, etc., and implements interfaces, which are also very convenient to write.

var b 
("Nick name")
(":")
("Zhiqiang 1224")
("\n")
("Contact QQ")
(":")
("354662600")
("\n")
(())

builder splicing

In order to improve the performance of buffer stitching, starting from the go 1.10 version, a builder type has been added to improve the performance of string stitching. It uses almost the same as buffer.

var b 
("Nick name")
(":")
("Zhiqiang 1224")
("\n")
("Contact QQ")
(":")
("354662600")
("\n")
(())

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 (p...)
}

func StringJoin(p []string) string{
  return (p,"")
}

func StringBuffer(p []string) string {
  var b 
  l:=len(p)
  for i:=0;i<l;i++{
   (p[i])
  }
  return ()
}

func StringBuilder(p []string) string {
  var b 
  l:=len(p)
  for i:=0;i<l;i++{
   (p[i])
  }
  return ()
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.