github.com/songzhibin97/gkit@v1.2.13/tools/bind/internal/bytesconv/bytesconv.go (about)

     1  // Copyright 2020 Gin Core Team. All rights reserved.
     2  // Use of this source code is governed by a MIT style
     3  // license that can be found in the LICENSE file.
     4  
     5  package bytesconv
     6  
     7  import (
     8  	"unsafe"
     9  )
    10  
    11  // StringToBytes converts string to byte slice without a memory allocation.
    12  func StringToBytes(s string) []byte {
    13  	return *(*[]byte)(unsafe.Pointer(
    14  		&struct {
    15  			string
    16  			Cap int
    17  		}{s, len(s)},
    18  	))
    19  }
    20  
    21  // BytesToString converts byte slice to string without a memory allocation.
    22  func BytesToString(b []byte) string {
    23  	return *(*string)(unsafe.Pointer(&b))
    24  }