In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly talks about "what are the splicing ways of strings in go language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the splicing ways of strings in the go language"?
+ splicing mode
This is the way I often use to write golang, go language with + splicing, php use. Concatenation, but because strings in golang are immutable types, concatenating with + results in a new string that has an impact on efficiency.
Func main () {S1: = "hello" S2: = "word" S3: = S1 + S2 fmt.Print (S3) / / S3 = "helloword"} sprintf function
S1: = "hello" S2: = "word" S3: = fmt.Sprintf ("% s% s", S1, S2) / / S3 = "helloword"
This approach is also often used in the development process, and the advantage of writing in this way is that it does not directly generate temporary strings, but it does not seem to be particularly efficient.
Join function
With the Join function, we need to introduce the strings package before we can call the Join function. The Join function will first calculate a concatenated length based on the contents of the string array, and then apply for the corresponding size of memory and fill it in a string. If there is already an array, this efficiency will be very high, and if not, the efficiency will not be high. I usually use it to slice and transfer strings.
S1: = "hello" S2: = "word" var str [] string = [] string {S1, S2} S3: = strings.Join (str, ") fmt.Print (S3) buffer.Builderbuffer.WriteString function
S1: = "hello" S2: = "word" var bt bytes.Bufferbt.WriteString (S1) bt.WriteString (S2) S3: = bt.String () fmt.Println (S3)
It's much more efficient than the one above, but I've basically never used it in development.
Buffer.Builder function
S1: = "hello" S2: = "word" var build strings.Builderbuild.WriteString (S1) build.WriteString (S2) S3: = build.String () fmt.Println (S3)
The stitching method recommended by the government is similar to that used above. The official suggestion is that I only like the first one as a rookie, so I usually use + stitching. If the stitching string is longer, it is the last way. After all, it is important to save your life.
Ps: using operators directly
Func BenchmarkAddStringWithOperator (b * testing.B) {hello: = "hello" world: = "world" for I: = 0; I < b.N; iTunes + {_ = hello + "," + world}}
The strings in golang are immutable, and each operation will produce a new string, so it will produce a lot of temporary useless strings, which will not only be useless, but also bring additional burden to gc, so the performance is relatively poor.
At this point, I believe you have a deeper understanding of "what are the splicing ways of strings in go language?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.