All Topics
All Topics
Technology
Technology
Design
Design
Programming
Programming
Science
Science
News
News
Gaming
Gaming
Entertainment
Entertainment
Business
Business
Finance
Finance
Sports
Sports
Health
Health
Food
Food
Travel
Travel
Art
Art
Music
Music
Books
Books
Education
Education
Politics
Politics
Personal
Personal
No algorithm. No AI slop. No ads. Just RSS. Pro-human. Indie writers. Real journalism. Open web. Chronological. Hand toasted.

Understanding Go Struct Embedding: Benefits and Potential Pitfalls

By

mattjhall

8mo ago· 1 min readen

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 pulled
Go 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 }
Snippet from the RSS feed
The gophers nested too greedily and too deep.

You might also wanna read