In the program below, 'i' is treated as not escaping, yet it does escape. This can
cause crashes and memory corruption.
package main
import (
"fmt"
)
type T struct {
v *int
}
func foo(ch chan *T) {
i := 42
t := T{&i}
defer func() {
ch <- &t
}()
}
func main() {
ch := make(chan *T, 2)
foo(ch)
foo(ch)
close(ch)
for t := range ch {
fmt.Printf("%v %v\n", t, *t.v)
}
}
In the program below, 'i' is treated as not escaping, yet it does escape. This can cause crashes and memory corruption. package main import ( "fmt" ) type T struct { v *int } func foo(ch chan *T) { i := 42 t := T{&i} defer func() { ch <- &t }() } func main() { ch := make(chan *T, 2) foo(ch) foo(ch) close(ch) for t := range ch { fmt.Printf("%v %v\n", t, *t.v) } }