util_unsafe.go (515B)
1 // +build !appengine,!js 2 3 package util 4 5 import ( 6 "reflect" 7 "unsafe" 8 ) 9 10 // BytesToReadOnlyString returns a string converted from given bytes. 11 func BytesToReadOnlyString(b []byte) string { 12 return *(*string)(unsafe.Pointer(&b)) 13 } 14 15 // StringToReadOnlyBytes returns bytes converted from given string. 16 func StringToReadOnlyBytes(s string) (bs []byte) { 17 sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) 18 bh := (*reflect.SliceHeader)(unsafe.Pointer(&bs)) 19 bh.Data = sh.Data 20 bh.Cap = sh.Len 21 bh.Len = sh.Len 22 return 23 }