github.com/go-playground/pkg/v5@v5.29.1/unsafe/conversions_go121.go (about) 1 //go:build go1.21 2 3 package unsafeext 4 5 import ( 6 "unsafe" 7 ) 8 9 // BytesToString converts an array of bytes into a string without allocating. 10 // The byte slice passed to this function is not to be used after this call as it's unsafe; you have been warned. 11 func BytesToString(b []byte) string { 12 return unsafe.String(unsafe.SliceData(b), len(b)) 13 } 14 15 // StringToBytes converts an existing string into an []byte without allocating. 16 // The string passed to these functions is not to be used again after this call as it's unsafe; you have been warned. 17 func StringToBytes(s string) (b []byte) { 18 d := unsafe.StringData(s) 19 b = unsafe.Slice(d, len(s)) 20 return 21 }