github.com/gogf/gf/v2@v2.7.4/util/gconv/gconv_unsafe.go (about)

     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  
     7  package gconv
     8  
     9  import "unsafe"
    10  
    11  // UnsafeStrToBytes converts string to []byte without memory copy.
    12  // Note that, if you completely sure you will never use `s` variable in the feature,
    13  // you can use this unsafe function to implement type conversion in high performance.
    14  func UnsafeStrToBytes(s string) []byte {
    15  	return *(*[]byte)(unsafe.Pointer(&s))
    16  }
    17  
    18  // UnsafeBytesToStr converts []byte to string without memory copy.
    19  // Note that, if you completely sure you will never use `b` variable in the feature,
    20  // you can use this unsafe function to implement type conversion in high performance.
    21  func UnsafeBytesToStr(b []byte) string {
    22  	return *(*string)(unsafe.Pointer(&b))
    23  }