小编典典

你如何清除 Go 中的切片?

go

在 Go 中清除切片的合适方法是什么?

这是我找到的内容:

// test.go
package main

import (
    "fmt"
)

func main() {
    letters := []string{"a", "b", "c", "d"}
    fmt.Println(cap(letters))
    fmt.Println(len(letters))
    // clear the slice
    letters = letters[:0]
    fmt.Println(cap(letters))
    fmt.Println(len(letters))
}

这样对吗?

澄清一下,缓冲区已清除,以便可以重用。

一个例子是bytes 包中的Buffer.Truncate函数。

请注意,Reset 只是调用了 Truncate(0)。因此,在这种情况下,第 70 行似乎会评估: b.buf = b.buf[0 : 0]

http://golang.org/src/pkg/bytes/buffer.go

// Truncate discards all but the first n unread bytes from the buffer.
60  // It panics if n is negative or greater than the length of the buffer.
61  func (b *Buffer) Truncate(n int) {
62      b.lastRead = opInvalid
63      switch {
64      case n < 0 || n > b.Len():
65          panic("bytes.Buffer: truncation out of range")
66      case n == 0:
67          // Reuse buffer space.
68          b.off = 0
69      }
70      b.buf = b.buf[0 : b.off+n]
71  }
72  
73  // Reset resets the buffer so it has no content.
74  // b.Reset() is the same as b.Truncate(0).
75  func (b *Buffer) Reset() { b.Truncate(0) }

阅读 397

收藏
2021-11-12

共1个答案

小编典典

这一切都取决于您对“清晰”的定义。有效的其中之一当然是:

slice = slice[:0]

但有一个问题。如果切片元素的类型为 T:

var slice []T

然后强制len(slice)为零,通过上述“技巧”, 不会使任何元素

slice[:cap(slice)]

符合垃圾收集条件。在某些情况下,这可能是最佳方法。但这也可能是“内存泄漏”的原因 - 内存未使用,但可能可以访问(在重新切片后),因此不是垃圾“可收集”。

2021-11-12