Be Careful with Go Struct Embedding Date: 2025-08-05 Go's struct embedding allows composing types by incorporating one struct into another, enabling direct access to embedded fields: The second Printf works because embedded struct fields are promoted for direct access, simplifying syntax. --- Ambiguity Pitfall with Nested Embedding Consider this example: Expected behavior: Compilation failure due to ambiguous URL field from both FooService and BarConnectionOptions. Actual behavior: Prints abc.com – the field from the least nested embedded struct (FooService). This subtlety can cause unexpected bugs, especially when different nested embedded structs share field names. --- Key Takeaways Go struct embedding promotes fields for ease of use. When multiple embedded structs contain fields with the same name, the least nested field shadows others. Ambiguities do not cause compilation errors; instead, Go resolves to the shallowest field. Be cautious with embedding to avoid unexpected field resolution in complex structs. Always test embedded struct field behavior rigorously to catch such issues early. --- This insight came from a real-world case and serves as a practical reminder to scrutinize use of struct embedding in Go.