We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
《如何实现字符串和byte切片的零拷贝转换》章节,在1.20版之后有变化了,原方法后面会不适用了。
新的方法:
func StringToBytes(s string) []byte { return unsafe.Slice(unsafe.StringData(s), len(s)) } func BytesToString(b []byte) string { return unsafe.String(&b[0], len(b)) }
The text was updated successfully, but these errors were encountered:
针对于 BytesToString 发现两个参考:
BytesToString
unsafe.String(unsafe.SliceData(b), len(b))
strings.Builder String()
unsafe.String(&b[0], len(b))
strings.Clone
不过需要注意使用后者的话,需要先判断长度
func BytesToString(b []byte) string { if len(b) == 0 { return "" } return unsafe.String(&b[0], len(b)) }
Sorry, something went wrong.
No branches or pull requests
问题描述
《如何实现字符串和byte切片的零拷贝转换》章节,在1.20版之后有变化了,原方法后面会不适用了。
新的方法:
The text was updated successfully, but these errors were encountered: