News for hackers. Stuff that matters.
Re:Be Careful with Go Struct Embedding by tymscar on Sunday September 21, 2025 @08:13PM (#45327847)
So I got curious and I looked at the compiler source code, and it does a depth-first search.

The fascinating bit to me is that there is a consolidateMultiples function in go/src/go/types/lookup.go (lines 286-304) that detects when multiple embedded types at the same depth provide the same field name. I wonder why they don’t do this for all levels. How deep could this even be in practice for it to matter? You could just have a hashmap with them all.

Re:Be Careful with Go Struct Embedding by dgl on Sunday September 21, 2025 @08:18PM (#45327878)
> I wonder why they don’t do this for all levels. How deep could this even be in practice for it to matter? You could just have a hashmap with them all.

While it may seem questionable for fields; it applies to methods too and is potentially more useful as a way to override them when doing struct embedding but wanting to preserve an interface.

Re:Be Careful with Go Struct Embedding by ankraft on Monday September 22, 2025 @08:37AM (#45332551)
It performs a breadth-first search, not a depth-first search.
Re:Be Careful with Go Struct Embedding by divan on Monday September 22, 2025 @08:54AM (#45332755)
To protect against changes in structs coming from external libraries. Or, better phrased, external structs should not dictate what names you're allowed to use in your own structs (so they have priority).