Re:a[low:high:max] in Golang – A Rare Slice Trick
by tedunangst on Saturday March 18, 2023 @02:37PM
(#35211841)
I don't think you can convert a string to []byte without unsafe except by copying?
[ Parent
| on HN ]
Re:a[low:high:max] in Golang – A Rare Slice Trick
by throwaway894345 on Saturday March 18, 2023 @10:45PM
(#35215476)
Agreed. You can copy a string into a byte slice, but I don’t think you can convert one into a byte slice that panics on mutation.
Re:a[low:high:max] in Golang – A Rare Slice Trick
by morelisp on Sunday March 19, 2023 @06:01AM
(#35217533)
Is it panicking on mutation because Go has some special logic, or is it panicking because the text segment is PROT_READ? If it's the latter, sure you can do that too.
Re:a[low:high:max] in Golang – A Rare Slice Trick
by throwaway894345 on Sunday March 19, 2023 @06:51PM
(#35224562)
I don’t know what PROT_READ is, but as far as I know, Go doesn’t allow you to convert a string into a byte slice apart from ‘unsafe’ (and presumably you’re free to modify that without panicking, but even if not, you used unsafe so the onus is on you to know what you’re doing). There’s syntax support for creating a byte slice by copying a string’s data, but that won’t panic on mutation.