了解Go的切片符号

时间:2018-07-28 22:08:27

标签: arrays go garbage-collection slice

我正在遍历Go-tour,this模块让我开始思考,似乎每次您通过对下限进行切片来修改视图时,切片的容量和长度都会减少。但是,如前所述,由切片创建的基础数组不会更改。

我将示例代码简化为:

package main

import "fmt"

func main() {
    s := []int{2, 3, 5, 7, 11, 13}

    s = s[6:]
    fmt.Println(cap(s), len(s), s)
}

这将显示:0 0 [],这意味着该切片现在完全没有用了。

但是,数组未更改,数组仍然是(出于可视化考虑):

[2, 3, 5, 7, 11, 13]

并由s引用,这意味着它不会收集垃圾。

所以我的问题是,这是切片的副作用还是此预期/首选行为?其次,是否有办法将视图还原为原始视图? (显示[2, 3, 5, 7, 11, 13]

2 个答案:

答案 0 :(得分:2)

您似乎了解Go切片。


Go slice被实现为struct

type slice struct {
    array unsafe.Pointer
    len   int
    cap   int
}

这是对底层数组的视图。

例如,

package main

import "fmt"

func main() {
    s := []int{2, 3, 5, 7, 11, 13}
    fmt.Println("s", cap(s), len(s), s)

    t := s[cap(s):]
    fmt.Println("s", cap(s), len(s), s)
    fmt.Println("t", cap(t), len(t), t)

    t = s
    fmt.Println("s", cap(s), len(s), s)
    fmt.Println("t", cap(t), len(t), t)
}

游乐场:php-cs-fixer

输出:

s 6 6 [2 3 5 7 11 13]
s 6 6 [2 3 5 7 11 13]
t 0 0 []
s 6 6 [2 3 5 7 11 13]
t 6 6 [2 3 5 7 11 13]

在没有对底层数组任何元素的引用(指针)之前,不会对底层数组进行垃圾回收。

例如,

package main

import "fmt"

func main() {
    s := []int{2, 3, 5, 7, 11, 13}
    fmt.Println("s", cap(s), len(s), s, &s[0])
    t := s
    // the slice s struct can be garbage collected
    // the slice s underlying array can not be garbage collected
    fmt.Println("t", cap(t), len(t), s, &t[0])
    p := &t[0]
    // the slice t struct can be garbage collected
    // the slice t (slice s) underlying array can not be garbage collected
    fmt.Println("p", p, *p)
    // the pointer p can be garbage collected
    // the slice t (and s) underlying array can be garbage collected
}

游乐场:https://play.golang.org/p/i-gufiJB-sP

输出:

s 6 6 [2 3 5 7 11 13] 0x10458000
t 6 6 [2 3 5 7 11 13] 0x10458000
p 0x10458000 2

阅读:

https://play.golang.org/p/PcB_IS7S3QE

The Go Blog: Go Slices: usage and internals

The Go Blog: Arrays, slices (and strings): The mechanics of 'append'The Go Programming Language SpecificationSlice types

答案 1 :(得分:2)

关于第二个问题,是的,您可以还原原始图像,但只能使用反射和不安全的非常黑暗的魔术。

hdr := (*reflect.SliceHeader)(unsafe.Pointer(&s)) //extract SliceHeader
arr := *(*[6]int)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&s)).Data)) //restore array data

playground

  

不能安全或便携地使用它,并且其表示形式可能会更改   在以后的版本中。

请勿在生产中使用它。

相关问题