bytes.go 391 B

123456789101112131415161718
  1. package myth
  2. import "unsafe"
  3. // StringToBytes converts string to byte slice without a memory allocation.
  4. func StringToBytes(s string) []byte {
  5. return *(*[]byte)(unsafe.Pointer(
  6. &struct {
  7. string
  8. Cap int
  9. }{s, len(s)},
  10. ))
  11. }
  12. // BytesToString converts byte slice to string without a memory allocation.
  13. func BytesToString(b []byte) string {
  14. return *(*string)(unsafe.Pointer(&b))
  15. }