Skip to content

Commit

Permalink
Replace slice header because it's deprecated since go 1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
tkxkd0159 committed Jun 7, 2024
1 parent efd42e3 commit 9a68c53
Showing 1 changed file with 1 addition and 8 deletions.
9 changes: 1 addition & 8 deletions internal/conv/string.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package conv

import (
"reflect"
"unsafe"
)

// UnsafeStrToBytes uses unsafe to convert string into byte array. Returned bytes
// must not be altered after this function is called as it will cause a segmentation fault.
func UnsafeStrToBytes(s string) []byte {
var buf []byte
sHdr := (*reflect.StringHeader)(unsafe.Pointer(&s))
bufHdr := (*reflect.SliceHeader)(unsafe.Pointer(&buf))
bufHdr.Data = sHdr.Data
bufHdr.Cap = sHdr.Len
bufHdr.Len = sHdr.Len
return buf
return unsafe.Slice(unsafe.StringData(s), len(s)) // ref https://github.com/golang/go/issues/53003#issuecomment-1140276077
}

// UnsafeBytesToStr is meant to make a zero allocation conversion
Expand Down

0 comments on commit 9a68c53

Please sign in to comment.