Hello everyone, I am fried fish.
When writing business code, you often have to deal with the content of strings. It is common to log in to the account by email. If it is: eddycjy@gmail.com , then you have to cut it according to @, take out the front and back, respectively, to identify the username and email address.
Is this kind of requirement convenient to write in Go? Today, I will let you know about it by Fried Fish.
background
duplicate code
Coincidentally, Ainar Garipov encountered the cutting needs we mentioned earlier in many projects.
For example:
idx = strings.Index(username, "@")
if idx != -1 {
name = username[:idx]
} else {
name = username
}
Or:
idx = strings.LastIndex(address, "@")
if idx != -1 {
host = address[idx+1:]
} else {
host = address
}
It is often necessary to repeatedly write some cumbersome code, and the proposer expresses unpleasantness.
new proposal
Implementation content
It is recommended to add the Cut method to the strings standard library:
func Cut(s, sep string) (before, after string, found bool) {
if i := Index(s, sep); i >= 0 {
return s[:i], s[i+len(sep):], true
}
return s, "", false
}
Synchronization is also in the bytes standard library:
func Cut(s, sep []byte) (before, after []byte, found bool)
This way, you can go from the original:
eq := strings.IndexByte(rec, '=')
if eq == -1 {
return "", "", s, ErrHeader
}
k, v = rec[:eq], rec[eq+1:]
become:
k, v, ok = strings.Cut(rec, "=")
if !ok {
return "", "", s, ErrHeader
}
It will be more elegant in writing, and will be more readable and abstract in complex scenarios.
Accept the reason
There may be some friends who will complain. Go actually created a new function just to save 1 line of code. Is this still the best way?
In fact, after the official team (Russ Cox) stepped in, he analyzed the main Go repository and searched for the use of related functions like:
- strings.Index。
- strings.IndexByte。
- strings.IndexRune。
After statistics, converted to strings.Cut
, there are 311 index calls outside the example and test data.
Some really unnecessary ones are excluded, leaving 285 calls. Of these calls, 221 are best written as the Cut method, which is more elegant.
That is to say, 77% of the existing Go code can be written more clearly with the new Cut function, and the readability and abstraction can be done better.
The fact that the main Go repository has such duplication of code, he thinks, is pretty incredible!
Summarize
Among the new features of Go1.18, Cut only adds a new method, which seems innocuous.
But usages like the Cut method have actually been invented twice in the major version of Go.
The emergence of this new method simultaneously replaces and simplifies most of the usage in four different standard library functions: Index, IndexByte, IndexRune, and SplitN.
For these reasons, Cut was eventually added to the standard library.
what do you think? :)
If you have any questions, please feedback and exchange in the comment area. The best relationship between . Your likes is fried fish create, thank you for your support.
The article is continuously updated, you can read it on search [Brain fried fish], this article 161f76cdb9ae2c GitHub github.com/eddycjy/blog has been included, to learn Go language, you can see Go learning map and route , welcome to Star.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。