github.com/mailgun/holster/v4@v4.20.0/unsafe/unsafe.go (about) 1 package unsafe 2 3 import ( 4 "unsafe" 5 ) 6 7 // BytesToString converts a byte slice to a string without allocation. The 8 // returned string reuses the slice byte array. Since Go strings are immutable, 9 // the bytes passed to BytesToString must not be modified afterwards. 10 func BytesToString(b []byte) string { 11 return unsafe.String(unsafe.SliceData(b), len(b)) 12 } 13 14 // StringToBytes converts a string to a byte slice without allocation. The 15 // returned byte slice reuses the underlying string byte array. Since Go 16 // strings are immutable, the bytes returned by StringToBytes must not be 17 // modified. 18 func StringToBytes(s string) []byte { 19 return unsafe.Slice(unsafe.StringData(s), len(s)) 20 }