go.sdls.io/sin@v0.0.9/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  	// #nosec G103
    14  	return *(*[]byte)(unsafe.Pointer(
    15  		&struct {
    16  			string
    17  			Cap int
    18  		}{s, len(s)},
    19  	))
    20  }
    21  
    22  // BytesToString converts byte slice to string without a memory allocation.
    23  func BytesToString(b []byte) string {
    24  	// #nosec G103
    25  	return *(*string)(unsafe.Pointer(&b))
    26  }