Re:Optimizing Go string operations with practical examples
by tedunangst on Saturday December 02, 2023 @08:48PM
(#38504049)
Cut only scans the string once (not twice) and doesn't allocate a slice to return.
[ Parent
| on HN ]
Re:Optimizing Go string operations with practical examples
by costco on Saturday December 02, 2023 @09:22PM
(#38504213)
Maybe I'm confused on how strings work but if this doesn't allocate:
// excerpted from Cut
if i := Index(s, sep); i >= 0 {
return s[:i], s[i+len(sep):], true
}
return s, "", false
Then why shouldn't this also not allocate? eoc = strings.Index(round, ", ")
if eoc == -1 {
currentScoreWithColor = round
} else {
currentScoreWithColor = round[:eoc]
round = round[eoc+2:]
}
Edit: oh, you're comparing it to Split. I was comparing it to what they ended up implementing.