There are many methods of string splicing supported in the go language. Here is a list
Commonly used string splicing methods
1. The most common method is definitely to concatenate two strings. (Recommended Learning: Go )
## This is similar to Python, but because the string in Golang is an indispensable type, a new character string pair with a connection will generate a connection. Efficiency has an impact.s1 := "字符串" s2 := "拼接" s3 := s1 + s2 fmt.Print(s3) //s3 = "打印字符串"
2. The second method uses the sprintf function, although it will not generate a temporary string like using it directly. But the efficiency is not high
s1 := "字符串" s2 := "拼接" s3 := fmt.Sprintf("%s%s", s1, s2) //s3 = "打印字符串"
3. The third method is to use the Join function. Here we need to introduce the strings package before calling the Join function.
The Join function will first calculate the length after splicing based on the contents of the string array, then apply for memory of the corresponding size and fill in the strings one by one. If there is already an array , this efficiency will be very high, otherwise it will not be efficient.//需要先导入strings包 s1 := "字符串" s2 := "拼接" //定义一个字符串数组包含上述的字符串 var str []string = []string{s1, s2} //调用Join函数 s3 := strings.Join(str, "") fmt.Print(s3)
The above is the detailed content of How to concatenate strings in golang. For more information, please follow other related articles on the PHP Chinese website!