We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 277c1bf commit a04c0ecCopy full SHA for a04c0ec
bytes_unsafe.go
@@ -6,6 +6,7 @@ import (
6
"reflect"
7
"strconv"
8
"unsafe"
9
+ "runtime"
10
)
11
12
//
@@ -32,11 +33,12 @@ func bytesToString(b *[]byte) string {
32
33
}
34
35
func StringToBytes(s string) []byte {
36
+ b := make([]byte, 0, 0)
37
+ bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
38
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
- bh := reflect.SliceHeader{
- Data: sh.Data,
- Len: sh.Len,
39
- Cap: sh.Len,
40
- }
41
- return *(*[]byte)(unsafe.Pointer(&bh))
+ bh.Data = sh.Data
+ bh.Cap = sh.Len
+ bh.Len = sh.Len
42
+ runtime.KeepAlive(s)
43
+ return b
44
0 commit comments