gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

bytesconv_1.19.go (580B)


      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 //go:build !go1.20
      6 
      7 package bytesconv
      8 
      9 import (
     10 	"unsafe"
     11 )
     12 
     13 // StringToBytes converts string to byte slice without a memory allocation.
     14 func StringToBytes(s string) []byte {
     15 	return *(*[]byte)(unsafe.Pointer(
     16 		&struct {
     17 			string
     18 			Cap int
     19 		}{s, len(s)},
     20 	))
     21 }
     22 
     23 // BytesToString converts byte slice to string without a memory allocation.
     24 func BytesToString(b []byte) string {
     25 	return *(*string)(unsafe.Pointer(&b))
     26 }