gconv_unsafe.go 930 B

1234567891011121314151617181920212223
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gconv
  7. import "unsafe"
  8. // UnsafeStrToBytes converts string to []byte without memory copy.
  9. // Note that, if you completely sure you will never use `s` variable in the feature,
  10. // you can use this unsafe function to implement type conversion in high performance.
  11. func UnsafeStrToBytes(s string) []byte {
  12. return *(*[]byte)(unsafe.Pointer(&s))
  13. }
  14. // UnsafeBytesToStr converts []byte to string without memory copy.
  15. // Note that, if you completely sure you will never use `b` variable in the feature,
  16. // you can use this unsafe function to implement type conversion in high performance.
  17. func UnsafeBytesToStr(b []byte) string {
  18. return *(*string)(unsafe.Pointer(&b))
  19. }