github.com/fufuok/balancer@v1.0.0/utils/convert.go (about)

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