On Sat, Apr 18, 2020 at 10:26 AM Miguel D <david.horiami...@gmail.com> wrote: > > Hello everyone. > I'm learning the go language and I have some questions regarding the > implementation of slices. > > $ cat slice_stack.go > package main > > import "fmt" > > func main() { > stack_array := [4]int{1,2,3,4} > // !- I assume this is on the stack, like a local int[4] would be in C. > > slice := stack_array[:] // Does this copy the data to the heap? > // or just get a slice to the mem on the stack? > for i := 0; i<25; i++ { > slice = append(slice, 99) // Does this copy the data to the heap on > first append? > } > > fmt.Println(stack_array) > fmt.Println(slice) > } > > > $ go run slice_stack.go > [1 2 3 4] > [1 2 3 4 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 > 99 99] > > Is it common to get slices to arrays on the stack? Am I wrong when I say > stack_array is on the stack?
Besides what other people said, I want to clarify that whether a value is on the stack or the heap is not a property of the language. Different implementations of Go can and do make different choices in this area. For the gc compiler that most people use, you can get information about whether a value is on the stack or the heap by using "-gcflags=-m". For your example that prints # command-line-arguments foo.go:15:16: inlining call to fmt.Println foo.go:16:16: inlining call to fmt.Println foo.go:6:5: moved to heap: stack_array foo.go:15:16: stack_array escapes to heap foo.go:15:16: []interface {} literal does not escape foo.go:16:16: slice escapes to heap foo.go:16:16: []interface {} literal does not escape <autogenerated>:1: .this does not escape So at present stack_array does escape to the heap. That may change in future releases. You can get a bunch more info using -gcflags=-m=2. Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcXyFBHM90JT4i1bRXjK-fSWgmgRLvoP67qR9ZVnuNR9_Q%40mail.gmail.com.