Understanding Go Struct Embedding: Benefits and Potential Pitfalls
By
mattjhall
Tired, dry, slightly forgotten on the back of the tray.
Summary
This article discusses Go programming language's struct embedding feature, explaining how it allows type composition and field promotion but warns about potential pitfalls when embedded structs have conflicting field names. The example shows how embedded Position and Colour structs allow direct access to their fields, but hints at problems when multiple embedded structs share the same field names like 'URL'.
Key quotes
· 5 pulledGo has a feature called struct embedding that allows you to compose types
This works: fmt.Printf("%d,%d\n", r.Position.X, r.Position.Y)
but so does this: fmt.Printf("%d,%d\n", r.X, r.Y)
But what do you think this code does?
type BarService struct { BarConnectionOptions }
You might also wanna read
Go 1.25 introduces experimental new JSON API packages after 15 years
The Go programming language team announces experimental support for new JSON packages (encoding/json/jsontext and encoding/json/v2) in Go 1.
Go 1.25 Testing/Synctest Package: Simplifying Asynchronous Code Testing
The article discusses the testing/synctest package in Go 1.25, which has graduated from experimental to general availability. It explains th
Why Average LLM Use Is Likely Destroying Value in Software Development
The author argues that, contrary to prevailing hype, the average use of Large Language Models (LLMs) is likely destroying value rather than
How AI Accelerated Prototyping: From Idea to Tangible in Record Time
The author reflects on how AI has transformed their prototyping workflow. Previously, the biggest bottleneck was the time needed to scaffold
GitLab 19.0 launches with Secrets Manager, agentic workflows, and self-hosted AI models
GitLab 19.0 has been released, positioning itself as an intelligent orchestration platform for DevSecOps. The release includes expanded secr
bit.ly·1d agoCentralizing Error Handling in Rust with Custom AppError Enums
This article discusses the importance of centralizing error handling in Rust applications using a custom AppError enum combined with map_err
