Skip to content

Instantly share code, notes, and snippets.

@mlsteele
Last active May 14, 2018 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlsteele/52b1e40455bd971e3a5794cf08383d35 to your computer and use it in GitHub Desktop.
Save mlsteele/52b1e40455bd971e3a5794cf08383d35 to your computer and use it in GitHub Desktop.
Don't take the address of an element of a slice
// 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