This code creates two interface variables from the same pointer. The prints demonstrate that they are the same pointer (as opposed to storing copies of s
and s2
). Yet, the last print says i1
is not the same as i2
. Why?
package main
import "fmt"
func main() {
var s T = &struct{string}{}
var s2 *struct{string} = s
var i1 interface{} = s
var i2 interface{} = s2
fmt.Println(s)
s.string = "s is i1"
fmt.Println(i1)
s.string = "s is i2"
fmt.Println(i2)
fmt.Println(i1==i2)
}
type T *struct{string}
$ go run a.go
&{}
&{s is i1}
&{s is i2}
false