bytesconv_1.20.go 738 B

1234567891011121314151617181920212223
  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. //go:build go1.20
  5. package bytesconv
  6. import (
  7. "unsafe"
  8. )
  9. // StringToBytes converts string to byte slice without a memory allocation.
  10. // For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077.
  11. func StringToBytes(s string) []byte {
  12. return unsafe.Slice(unsafe.StringData(s), len(s))
  13. }
  14. // BytesToString converts byte slice to string without a memory allocation.
  15. // For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077.
  16. func BytesToString(b []byte) string {
  17. return unsafe.String(unsafe.SliceData(b), len(b))
  18. }