github.com/kaiya/goutils@v1.0.1-0.20230226104005-4ae4a4dc3688/strings/string.go (about)

     1  package strings
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  )
     7  
     8  func String2Bytes(s string) []byte {
     9  	sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
    10  	bh := reflect.SliceHeader{
    11  		Data: sh.Data,
    12  		Len:  sh.Len,
    13  		Cap:  sh.Len,
    14  	}
    15  	return *(*[]byte)(unsafe.Pointer(&bh))
    16  }
    17  
    18  func Bytes2String(b []byte) string {
    19  	return *(*string)(unsafe.Pointer(&b))
    20  }