1 Star 0 Fork 0

kade / librarys

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
bytesconv.go 547 Bytes
一键复制 编辑 原始数据 按行查看 历史
kade 提交于 2024-03-26 16:58 . v1
package bytes
import (
"unsafe"
)
// StringToBytes converts string to byte slice without a memory allocation.
func StringToBytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(
&struct {
string
Cap int
}{s, len(s)},
))
}
//由于 string 类型的底层表示是一个指向字节数组的指针,因此可以通过 unsafe.Pointer 和类型转换来绕过这些限制。
// BytesToString converts byte slice to string without a memory allocation.
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
1
https://gitee.com/go-kade/librarys.git
git@gitee.com:go-kade/librarys.git
go-kade
librarys
librarys
70cb23a27fa4

搜索帮助