On Tue, Apr 21, 2020 at 8:17 AM T L <tapir....@gmail.com> wrote: > > And is the runtime.KeepAlive call in the following program essential to keep > the correctness? > > package main > > import ( > "fmt" > "unsafe" > "reflect" > "runtime" > ) > > func main() { > a := [6]byte{'G', 'o', 'o', 'g', 'l', 'e'} > bs := []byte("Golang") > hdr := (*reflect.SliceHeader)(unsafe.Pointer(&bs)) > hdr.Data = uintptr(unsafe.Pointer(&a)) > > runtime.KeepAlive(&a) // Is this line essential here? > > hdr.Len = 2 > hdr.Cap = len(a) > fmt.Printf("%s\n", bs) // Go > bs = bs[:cap(bs)] > fmt.Printf("%s\n", bs) // Google > }
I do not think the runtime.KeepAlive is required. Setting the Data field using a *SliceHeader should keep the value alive. Ian -- 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/CAOyqgcU6g58mS4doqwVE6%2B3ejHxgtt0hHVeG6c44ctBNZi7DBg%40mail.gmail.com.