Explorar o código

add UnsafeCastString

Tao Wen %!s(int64=7) %!d(string=hai) anos
pai
achega
05fbef0ca5
Modificáronse 1 ficheiros con 10 adicións e 0 borrados
  1. 10 0
      reflect2.go

+ 10 - 0
reflect2.go

@@ -283,3 +283,13 @@ func NoEscape(p unsafe.Pointer) unsafe.Pointer {
 	x := uintptr(p)
 	return unsafe.Pointer(x ^ 0)
 }
+
+func UnsafeCastString(str string) []byte {
+	stringHeader := (*reflect.StringHeader)(unsafe.Pointer(&str))
+	sliceHeader := &reflect.SliceHeader{
+		Data: stringHeader.Data,
+		Cap: stringHeader.Len,
+		Len: stringHeader.Len,
+	}
+	return *(*[]byte)(unsafe.Pointer(sliceHeader))
+}