Created
November 9, 2015 15:27
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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