I have been using Go for an old Advent of Code and I keep running into the same surprise (at least, surprising to me from a Java and Rust background). I wrote a small "puzzler" that I think this community might enjoy:
package main import "fmt" type SelfReference struct { self *SelfReference } func make() SelfReference { s := SelfReference{} s.self = &s return s } func main() { s := make() fmt.Println(s == *s.self, &s == s.self) }
What does this program output? I get true false. What I think is happening is the make() function implicitly copies the struct. The original and the copy contain a reference to the same original object, but they aren't actually the same object. Is that what's going on or is there more to this?