20
golang面试题:字符串转成byte数组,会发生内存拷贝吗?
source link: https://studygolang.com/articles/29010
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
字符串转成byte数组,会发生内存拷贝吗?
字符串转成切片,会产生拷贝。严格来说,只要是发生类型强转都会发生内存拷贝。那么问题来了。
频繁的内存拷贝操作听起来对性能不大友好。 有没有什么办法可以在字符串转成切片的时候不用发生拷贝呢?
代码实现
package main import ( "fmt" "reflect" "unsafe" ) func main() { a :="aaa" ssh := *(*reflect.StringHeader)(unsafe.Pointer(&a)) b := *(*[]byte)(unsafe.Pointer(&ssh)) fmt.Printf("%v",b) } 复制代码
-
StringHeader
是字符串
在go的底层结构。
type StringHeader struct { Data uintptr Len int } 复制代码
-
SliceHeader
是切片
在go的底层结构。
type SliceHeader struct { Data uintptr Len int Cap int } 复制代码
-
那么如果想要在底层转换二者,只需要把
StringHeader
的地址强转成SliceHeader
就行。那么go有个很强的包叫unsafe
。-
1.
unsafe.Pointer(&a)
方法可以得到变量a
的地址。 -
2.
(*reflect.StringHeader)(unsafe.Pointer(&a))
可以把字符串a转成底层结构的形式。 -
3.
(*[]byte)(unsafe.Pointer(&ssh))
可以把ssh底层结构体转成byte的切片的指针。 -
4.再通过
*
转为指针指向的实际内容。
-
如果喜欢我的文章,可关注我的公众号:golang小白成长记
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK