Skip to content

Instantly share code, notes, and snippets.

@j16r
Created November 9, 2015 15:27
Show Gist options
  • Save j16r/afc0042003f8ee88deae to your computer and use it in GitHub Desktop.
Save j16r/afc0042003f8ee88deae to your computer and use it in GitHub Desktop.
I was curious if go allows for dangling pointers when you resize a collection
package main
import "fmt"
func main() {
collection := []int{1, 2, 3, 4, 5}
ptr1 := &collection[0]
ptr2 := &collection[1]
ptr3 := &collection[2]
ptr4 := &collection[3]
ptr5 := &collection[4]
for i := 0; i < 1000; i++ {
fmt.Println(*ptr1, *ptr2, *ptr3, *ptr4, *ptr5)
collection = append(collection, 9)
fmt.Println(ptr1, ptr2, ptr3, ptr4, ptr5, &collection)
throwaway := make([]int, 10)
throwaway[0] = 9
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment