Skip to content

Commit ecab8fa

Browse files
committed
Perform conversion from string to []byte according to rule #6 at https://golang.org/pkg/unsafe/#Pointer
1 parent 44caf1e commit ecab8fa

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ func b2s(b []byte) string {
99
return *(*string)(unsafe.Pointer(&b))
1010
}
1111

12-
func s2b(s string) []byte {
12+
func s2b(s string) (b []byte) {
1313
strh := (*reflect.StringHeader)(unsafe.Pointer(&s))
14-
var sh reflect.SliceHeader
14+
sh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
1515
sh.Data = strh.Data
1616
sh.Len = strh.Len
1717
sh.Cap = strh.Len
18-
return *(*[]byte)(unsafe.Pointer(&sh))
18+
return b
1919
}
2020

2121
const maxStartEndStringLen = 80

0 commit comments

Comments
 (0)