You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
607 B
Go
28 lines
607 B
Go
2 years ago
|
package hack
|
||
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
"unsafe"
|
||
|
)
|
||
|
|
||
|
// no copy to change slice to string
|
||
|
// use your own risk
|
||
|
func String(b []byte) (s string) {
|
||
|
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
||
|
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
||
|
pstring.Data = pbytes.Data
|
||
|
pstring.Len = pbytes.Len
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// no copy to change string to slice
|
||
|
// use your own risk
|
||
|
func Slice(s string) (b []byte) {
|
||
|
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
||
|
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
||
|
pbytes.Data = pstring.Data
|
||
|
pbytes.Len = pstring.Len
|
||
|
pbytes.Cap = pstring.Len
|
||
|
return
|
||
|
}
|