我的问题是切片长度和容量。我在这里学习:https://tour.golang.org/moretypes/11。

(我的问题被标记为可能的副本;但事实并非如此。我的问题特别是关于切掉切片的前几个元素及其含义。)

s = s[:4]s = s[:0]不降低容量时,线路s = s[2:]为什么会降低容量?我看到的唯一区别是,在s = s[2:]中,结肠前有一个数字,而在其他两行中,结肠后有一个数字。

有没有办法恢复我们与s = s[2:]切断的前两个元素?

单击"运行"按钮后,我们得到以下信息。


您可以在这里阅读更多关于切片的信息。但我认为这一段回答了你的问题:

Slicing does not copy the slice's data. It creates a new slice value that points to the original array. This makes slice operations as efficient as manipulating array indices. Therefore, modifying the elements (not the slice itself) of a re-slice modifies the elements of the original slice.

因此,如果将切片数据分配给同一个变量,则无法恢复切片数据。

容量减少是因为通过删除前2个元素,您将更改指向新切片的指针(切片由指向第一个元素的指针引用)。

如何在内存中表示切片:。

氧化镁

氧化镁