4 ms·
Another useful unsafe slice operation is casting a byte slice to a string so you can e.g. use it as a map key. Watch your lifetimes though! You probably also w
by ryanworl 7y ago
Another useful unsafe slice operation is casting a byte slice to a string so you can e.g. use it as a map key. Watch your lifetimes though!
You probably also wouldn’t want to use any string manipulation functions on it, but I’ve never had a reason to do that anyway.
- BarkMore 7y agoThe compiler optimizes conversion from []byte to string on map lookup. https://github.com/golang/go/commit/f5f5a8b6209f84961687d993b93ea0d397f5d5bf https://github.com/golang/go/commit/f5f5a8b6209f84961687d993...
- ryanworl 7y agoThat’s awesome! I’ll have to benchmark that and see if I can delete some code now. Thanks.
- Zach_the_Lizard 7y agoIt doesn't seem to do it in the general non-map, non-mutation case, though this optimization is welcome. I've personally resorted to using `unsafe` trickery in tight loops to overcome this limitation.