Last active
May 14, 2018 21:42
-
-
Save mlsteele/52b1e40455bd971e3a5794cf08383d35 to your computer and use it in GitHub Desktop.
Don't take the address of an element of a slice
This file contains hidden or 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
// https://play.golang.org/p/ZVV6drJRqva | |
package main | |
import ( | |
"fmt" | |
) | |
type S struct { | |
i int64 | |
} | |
func main() { | |
var s1 *S | |
var s2 *S | |
var ll []S | |
for i := int64(0); i < 200; i++ { | |
fmt.Printf("iteration %v\n", i) | |
ll = append(ll, S{i + 1000000}) | |
const start = 40 | |
if i == start { | |
s1 = &ll[3] | |
} | |
if i > start { | |
s2 = &ll[3] | |
if s1 != s2 { | |
panic(fmt.Errorf("%p != %p", s1, s2)) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment