- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在阅读 Go slice 时,我在 append
方法的上下文中遇到了这种行为
If the backing array of s is too small to fit all the given values a bigger array will be allocated. The returned slice will point to the newly allocated array.
Source - Golang Tour
为了理解这一点,我编写了以下代码:
func makeSlices() {
var a []int;
a = append(a, 0)
b := append(a, 1)
printSlice("b", b)
c := append(a, 2)
printSlice("b", b)
printSlice("c", c)
}
func printSlice(name string, s []int) {
fmt.Printf("var=%v len=%d cap=%d first_address=%v %v\n", name, len(s), cap(s), &s[0], s)
}
输出:
var=b len=2 cap=2 first_address=0x414020 [0 1]
var=b len=2 cap=2 first_address=0x414020 [0 2]
var=c len=2 cap=2 first_address=0x414020 [0 2]
我希望 b
和 c
指向相同的底层数组,因为它们都是相同长度的 slice
但是如果我要为另一个长度的 slice 改变相同的代码:
func makeSlices() {
var a []int;
a = append(a, 0, 9)
d := append(a, 1, 2)
printSlice("d", d)
e := append(a, 3, 4)
printSlice("d", d)
printSlice("e", e)
}
输出:
var=d len=5 cap=8 first_address=0x450020 [0 0 9 1 2]
var=d len=5 cap=8 first_address=0x450020 [0 0 9 1 2]
var=e len=5 cap=8 first_address=0x450040 [0 0 9 3 4]
在这种情况下,d
和 e
应该指向相同的后备数组,因为它们又是相同长度的 slice ,但它们不是。
为什么会出现这种行为异常? Go 到底什么时候决定为 slice 分配一个新的支持数组?
最佳答案
答案很简单:如果要追加的元素不适合当前容量,append()
会分配一个新的后备数组(并复制当前内容)。正式地:
if len(s) + len(newElements) > cap(s) {
// Allocate new backing array
// copy content (s) over to new array
} else {
// Just resize existing slice
}
// append (copy) newElements
例如,如果 len=2,cap=4,您可以附加 2 个元素,无需分配。
如果len=2,cap=4,追加3个元素,则len+3 > cap,所以会分配一个新的backing array(容量会大于len+3,考虑到 future 的增长,但是它的长度将是 2+3=5)。
在您的第一个示例中,您声明了一个长度和容量为 0 的 slice 变量。
var a []int
fmt.Println(len(a), cap(a)) // Prints 0 0
当你第一次追加时,一个新的数组将被分配:
a = append(a, 0)
fmt.Println(len(a), cap(a)) // Prints 1 2
当你做另一个追加时,它适合容量,所以没有分配:
fmt.Println(len(a), cap(a)) // Prints 1 2
b := append(a, 1)
fmt.Println(len(b), cap(b)) // Prints 2 2
但是这次你将结果 slice 存储在 b
中,而不是 a
中。因此,如果您对 a
进行第三次追加,它仍然具有 length=1 和 cap=2,因此将另一个元素追加到 a
将不需要分配:
fmt.Println(len(a), cap(a)) // Prints 1 2
c := append(a, 2)
fmt.Println(len(c), cap(c)) // Prints 2 2
因此除了第一个追加之外,所有其他追加不需要分配,因此第一个分配的后备数组用于所有 a
、b
和 c
slice ,因此它们第一个元素的地址将是相同的。这就是您所看到的。
您再次创建一个空 slice (len=0,cap=0)。
然后你做第一个追加:2个元素:
a = append(a, 0, 9)
fmt.Println(len(a), cap(a)) // Prints 2 2
这会分配一个长度为 2 的新数组,因此 slice 的长度和容量都将为 2。
然后你做你的第二次追加:
d := append(a, 1, 2)
fmt.Println(len(d), cap(d)) // Prints 4 4
由于没有空间容纳更多元素,因此分配了一个新数组。但是您将指向这个新数组的 slice 存储在 d
中,而不是 a
中。 a
仍然指向旧数组。
然后您进行第三次追加,但是添加到 a
(指向旧数组):
fmt.Println(len(a), cap(a)) // Prints 2 2
e := append(a, 3, 4)
fmt.Println(len(e), cap(e)) // Prints 4 4
同样,a
的数组无法容纳更多元素,因此分配了一个新数组,您将其存储在 e
中。
所以 d
和 e
有不同的后备数组,并且附加到任何与“另一个” slice 共享后备数组的 slice 不会(不能)改变这个“另一个” slice 。所以结果是您两次看到 d
的相同地址,以及 e
的不同地址。
关于go - Go什么时候分配一个新的后备数组给slice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55041641/
我正在使用 go 图表库 https://github.com/wcharczuk/go-chart制作条形图。我面临的问题是标签值很长,我想将文本旋转 45 度以显示完整文本 我喜欢显示的日期格式是
我在构建一个非常简单的通过 cgo 调用 c 代码的 go 程序时遇到了问题。我的设置: $: echo $GOPATH /go $: pwd /go/src/main $: ls ctest.c
没有 C 的背景,只有 Go 的“初学者”经验,我正在尝试弄清楚 main.go 是实际需要的还是只是一个约定。 我想创建一个简单的网络 API,但有人可以为我澄清一下吗? 最佳答案 main.go
我read从 Go 1.4 开始,Go 运行时是用 Go 本身编写的(而不是用 C)。 这怎么可能?如果 Go 程序在运行时之上运行,并且运行时是 Go 程序,那么运行时是否在自身之上运行? 最佳答案
这是“Go 之旅”中的代码示例 Range and Close : package main import ( "fmt" ) func fibonacci(n int, c chan int
给定以下 go.mod 文件: module foo go 1.12 require ( github.com/bar/baz v1.0.0 github.com/rat/cat v1
我有一个 CI/CD 管道,它需要跨平台并与几个不同的管理程序一起工作。为了不必更改 Windows 和 Linux 的构建任务,我认为 Go 将是编写一次代码并在任何地方运行的好方法。然而,考虑到
我有一个 Dockerfile,用于使用 go build 编译 Go 应用程序。我进行了研究,确实建议将 go build 用于生产。 但是我找不到正确的答案来解释为什么。 我了解 go run 创
我尝试在命令提示符#Go lang 中运行该程序-但是当我键入运行“go run hello.go”命令时,我开始了 CreateFile hello.go:The system cannot fin
我正在使用“Go 编程语言”一书学习 Go。第一章介绍os.Open用于读取文件的模块。我尝试打开如下所示的 go 文件。 f, err = os.Open("helloworld.go") 我收
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题?通过 editing this post 添加详细信息并澄清问题. 2年前关闭。 Improve this
为了解决我对 goroutine 的一些误解,我去了 Go 操场跑了 this code : package main import ( "fmt" ) func other(done cha
这个问题在这里已经有了答案: Evaluate/Execute Golang code/expressions like js' eval() (5 个回答) 1年前关闭。 对于任何 go 程序,我想
这是我基本上试图从路径打印基准的代码。 这意味着,如果用户输入“/some/random/path.java”,则输出将为“path”。同样,如果用户arg为“/another/myapp.c”,则输
$ go version 1.13.3 我的文件夹结构如下: GOPATH +---src +--- my-api-server +--- my-auth-server
这个问题在这里已经有了答案: How to embed file for later parsing execution use (4 个答案) What's the best way to bun
我觉得这有点奇怪,为什么这段代码不起作用? package main import "fmt" func main() { var i, j int = 1, 2 k
go编译器执行完如下命令后的可执行文件存放在哪里? $> go run file.go 最佳答案 在 /tmp 文件夹中,如果您使用的是 unix 机器。 如果您使用的是 Windows,则在 \Us
我目前正在开始使用 Go,并且已经深入研究了有关包命名和工作区文件夹结构的注意事项。 不过,我不太确定如何根据 Go 范式正确组织我的代码。 这是我当前的结构示例,它位于 $GOPATH/src 中:
假设我有一个接受用户输入的 Lua 程序,而该输入恰好是有效的 Lua 源代码。这是在程序仍在运行时进行清理、编译和执行的。 Go 是否(或将)实现这样的事情? 最佳答案 我认为以下两个项目之间有足够
我是一名优秀的程序员,十分优秀!