On Sat, 20 Dec 2025 at 03:24, [email protected] <[email protected]> wrote:
> Alternatively, if this kind of slice type conversion is truly disallowed,
> can anyone suggest a better way of writing the above function that
> maintains its readability and performance?
>
Oh and I would probably write the conversion instead as (possibly in a
function, optionally just using len directly instead of reslicing, if you
don't care about the difference):
func convert(s []Symbol) []byte {
return unsafe.Slice((*byte)(unsafe.SliceData(s)), cap(s))[:len(s)]
}
This is still using unsafe and it is more to type/read, but it is much
safer. In particular, it stops compiling if you ever change `Symbol` to no
longer have underlying type `byte`. You could also use generics to
get/reinforce that safety:
func convert[T ~byte](s []T) []byte {
return *(*[]byte)(unsafe.Pointer(&s))
}
(or combine both)
>
> Thanks
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion visit
> https://groups.google.com/d/msgid/golang-nuts/5bb6ca8a-9a69-4262-a2f5-c6fb109a40a4n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/5bb6ca8a-9a69-4262-a2f5-c6fb109a40a4n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfG1okmgeVx3g5MQEOiW7gr6oACdDk1-hATYoyKpkhD6Dw%40mail.gmail.com.