I think it could be a useful optimization. There are at least in two different (and much more general) proposals that, if accepted, would include readonly slices as a special case. The the compiler could recognize that a readonly slice created from a string can reuse string's underlying data:
* proposal: Go 2: read-only types https://github.com/golang/go/issues/22876 proposes a new 'ro' keyword to mark readonly values * proposal: Go 2: immutable type qualifier https://github.com/golang/go/issues/27975 proposes to allow marking with 'const' values (including function/method parameters) and field declarations On Friday, July 5, 2019 at 4:27:58 AM UTC+2, Ally Dale wrote: > > []byte is mutable in Go, but string is readonly, so []byte(string) in Go > is actually allocate&memcopy, and this operation is costly. > Sometimes, we need a readonly []byte parameter but we have string only, > and we have no choice but allocate&memcopy to make a new []byte variable > from string. > Eg: net.Send([]byte("ping")) is costly, but net.Send(readonly > []byte("ping")) can be cheaply. > -- 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 golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9e489307-cab8-4875-a33b-aa5ba99ed330%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.