func String2ByteSlice(str string) (bs []byte) {
    strHdr := (*reflect.StringHeader)(unsafe.Pointer(&str))
    sliceHdr := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
    
    // Is it possible that the str value is allocated on stack
    // and the stack grows at this moment, so the address value
    // stored in strHdr.Data will become obsoleted?
    
    sliceHdr.Data = strHdr.Data
    sliceHdr.Len = strHdr.Len
    sliceHdr.Cap = strHdr.Len
    
    runtime.KeepAlive(&str)
    
    return
}

-- 
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/237ef013-c11e-47e9-9436-d2bf59c0b5e0%40googlegroups.com.

Reply via email to