slice的底层结构是不是又变了?不是SliceHeader了?

slice的底层结构是不是又变了?不是SliceHeader了?
是变成unsafeheader.Slice了吗?

看源码:https://github.com/golang/go/...

另:go官方github仓库是不能搜索代码吗?搜一下结果里面code类别里面都是空的。

阅读 1.9k
1 个回答

是,1.20版本中被修改了,原有的reflect.SliceHeaderreflect.StringHeader被弃用。如注释所说,新类型的指针是unsafe.Pointer而非uintptr,更安全,不会被GC回收。

...
// Deprecated: Use unsafe.Slice or unsafe.SliceData instead.
type SliceHeader struct {
    ...
...
// Unlike reflect.SliceHeader, its Data field is sufficient to guarantee the
// data it references will not be garbage collected.
type Slice struct {
    ...
The unsafe package defines three new functions SliceData, String, and StringData. Along with Go 1.17's Slice, these functions now provide the complete ability to construct and deconstruct slice and string values, without depending on their exact representation.

官方提供了三个新函数帮助完成字符串和切片数组的转化,unsafe.SliceDataunsafe.Stringunsafe.StringData

详见:
Go 1.20 发行说明
unsafe:新增 StringData, String, SliceData

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题