bytesconv_1.20.go (738B)
1 // Copyright 2023 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 // For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077. 15 func StringToBytes(s string) []byte { 16 return unsafe.Slice(unsafe.StringData(s), len(s)) 17 } 18 19 // BytesToString converts byte slice to string without a memory allocation. 20 // For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077. 21 func BytesToString(b []byte) string { 22 return unsafe.String(unsafe.SliceData(b), len(b)) 23 }