# 重新思考 Go：Slice 只是「操作视图」

DevFeed: [重新思考 Go：Slice 只是「操作视图」](<https://devfeed.tech/articles/go-slice-40989.md>)

Original publisher: [Read original article](<https://blog.joway.io/posts/golang-rethink-slice/>)

Author: Joway

Published: 2024-03-30T00:00:00Z

Content type: article

Language: zh

Sources: [Random Thoughts](<https://devfeed.tech/sources/random-thoughts.md>)

Topics: [Go Language](<https://devfeed.tech/topics/go-language.md>), [bug](<https://devfeed.tech/topics/bug.md>)

Tags: [copy](<https://devfeed.tech/tags/copy.md>), [gc](<https://devfeed.tech/tags/gc.md>), [go](<https://devfeed.tech/tags/go.md>), [slices](<https://devfeed.tech/tags/slices.md>), [tech](<https://devfeed.tech/tags/tech.md>)

## AI overview

This article examines Go slices as views over underlying memory rather than memory representations. It explains how subslices can retain oversized buffers, pointer elements can remain referenced after removal, and certain allocation patterns can cause unnecessary memory clearing. It concludes by discussing Go 1.21's slices package as a safer way to manipulate slices.

## Source excerpt

重新思考 Go 系列：这个系列希望结合工作中在 Go 编程与性能优化中遇到过的问题，探讨 Go 在语言哲学、底层实现和现实需求三者之间关系与矛盾。 Go 在语法级别上提供了 Slice 类型作为对底层内存的一个「操作视图」: var sh []any // ==> internal struct of []any type SliceHeader struct { Data uintptr Len int Cap int } 编程者可以使用一些近似 Python 的语法来表达对底层内存边界的控制: