frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Be Careful with Go Struct Embedding

https://mattjhall.co.uk/posts/be-careful-with-go-struct-embedding.html
28•mattjhall•1h ago

Comments

tymscar•55m ago
That’s actually crazy. Why is this even a feature?
jrockway•48m ago
Because

   type Foo struct { 
       sync.Mutex
       whatever string
   }
   var foo Foo
   foo.Lock()
   foo.whatever = 42
   foo.Unlock()
is convenient.
echelon•36m ago
It's dangerous. This is awful.

Any coding construct that can cause defects is an antipattern. Your language should discourage defects by design.

This is like NULLs and "goto".

thrill•25m ago
Hmm, never realized the convenience came this way. Seems the compiler could emit a warning if two equal depth names might cause confusion, which could be ignored if acceptable.
sethammons•18m ago
almost always, the recommendation is to not embed your mutex; give it a name.

foo.mu.Lock()

This way you don't expose your primitives, preventing poor usage from causing a deadlock. Generally you don't want the user of your struct to have to know when or when to not lock.

mikepurvis•48m ago
At risk of being excessively sassy this looks like a case of wanting the ergonomics of multiple inheritance without fully grappling with the complexities or implications of it.
gdbsjjdn•34m ago
In most cases people just want any inheritance, this is the backwards way the Golang devs decided to implement it based on their 80s view of programming languages.
metadat•46m ago
IMHO it should be a compiler error. This is just so loose... a wheel fell off.
ShroudedNight•18m ago
A wheel is generous. This seems more like inviting the computing equivalent of spilling twenty thousand tons of crude into the sea, which then promptly catch fire.
dgl•31m ago
See how it's used in the standard library io types, it makes for quite nice composition: https://go.googlesource.com/go/+/refs/heads/master/src/io/io...
mananaysiempre•23m ago
I’m sympathetic to parts of the Go design philosophy, but the only thing that comes to mind looking at this is “damn, that’s some awkward syntax for (structural) intersection types”.

(It also feels to me that this sort of anonymous embedding is materially different for interfaces vs structs, though I admit that from a type-theoretic perspective it’s not.)

mananaysiempre•29m ago
At the very least, the Go authors have been convinced this should be a feature since the Plan 9 C dialect[1].

[1] http://doc.cat-v.org/plan_9/4th_edition/papers/comp, look for “anonymous structure or union” and note that a (different) part of that extension has since been standardized.

Olreich•45m ago
If you need to grab a particular struct's version of the data, you can via `opts.BarService.URL` or `opts.FooService.URL`: https://go.dev/play/p/MUSYJhmoC2D

Still worth being careful, but it can be useful when you have a set of common fields that everything of a certain group will have (such as a response object with basic status, debug info, etc. and then additional data based on the particular struct). I don't know why they let you embed multiple layers and multiple objects though. I've never gotten value out of anything but a "here's a single set of common fields struct embedding".

tymscar•39m ago
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.

dgl•34m ago
> 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.

linhan_dot_dev•35m ago
I like the Go language because it's straightforward and clear, even if it looks a bit plain.

I hope the feature mentioned in the article will cause a compiler error.

However, I wouldn't use this approach when writing my own code.

digianarchist•27m ago
I'm surprised this wasn't in the recent post submitted here: https://blog.habets.se/2025/07/Go-is-still-not-good.html

It's a one of a few rough edges in Go.

bilbo-b-baggins•24m ago
Huh my IDE linter spits out warnings about this. Not sure which extension does it.

A Plea for Salvation

https://open.substack.com/pub/faithkekana/p/a-plea-for-salvation
1•faiKekana•2m ago•0 comments

Picturing a Chinatown Family Across Twenty-Two Years

https://www.newyorker.com/culture/photo-booth/picturing-a-chinatown-family-across-twenty-two-years
1•prismatic•5m ago•0 comments

During WWII, the U.S. government censored the weather

https://www.popsci.com/environment/censor-weather-world-war-2-us/
1•bookofjoe•5m ago•0 comments

Is MCP a Security Nightmare?: A Look into MCP Authorization with OAuth2

https://cefboud.com/posts/mcp-oauth2-security-authorization/
1•mooreds•6m ago•0 comments

The messy reality of feeding Alaska

https://www.hcn.org/issues/57-9/the-messy-reality-of-feeding-alaska/
2•mooreds•6m ago•0 comments

MongoDB AMP

https://redmonk.com/rstephens/2025/09/16/mongodb-amp/
1•mooreds•7m ago•0 comments

One-shotting Minecraft in Three.js with GPT-5-codex

https://twitter.com/JasonBotterill3/status/1969730846417629277
1•birriel•11m ago•0 comments

An Engineer Says He's Found a Way to Overcome Earth's Gravity

https://www.popularmechanics.com/space/rockets/a65924333/engineer-overcoming-earths-gravity/
2•Stevvo•15m ago•0 comments

Perplexity for Government

https://www.perplexity.ai/hub/blog/introducing-perplexity-for-government
1•Brysonbw•17m ago•0 comments

Taking a Look at Compression Algorithms

https://cefboud.com/posts/compression/
3•ashvardanian•20m ago•0 comments

We Politely Insist: Your LLM Must Learn the Persian Art of Taarof

https://arxiv.org/abs/2509.01035
1•chosenbeard•21m ago•0 comments

Nicotinamide for Skin Cancer Chemoprevention

https://jamanetwork.com/journals/jamadermatology/article-abstract/2838591
1•wjb3•24m ago•1 comments

Horst Wessel

https://en.wikipedia.org/wiki/Horst_Wessel
1•rolph•25m ago•0 comments

String-Based CPU

https://www.halfbakery.com/idea/String-Based_20CPU
1•xk3•26m ago•0 comments

The 21 grams experiment that tried to weigh a human soul

https://www.popsci.com/science/21-grams-experiment/
1•domofutu•27m ago•0 comments

Linux 6.18 Expected to Land Google's Rust Binder Driver

https://www.phoronix.com/news/Rust-Binder-For-Linux-6.18
2•CharlesW•27m ago•0 comments

How to Spot Malicious Two-Factor Authentication Prompts

https://au.lifehacker.com/security/115825/feature/how-to-spot-malicious-two-factor-authentication...
2•domofutu•28m ago•0 comments

Lachlan Murdoch, Michael Dell, Ellison Involved in TikTok Deal

https://www.reuters.com/world/china/trump-says-chinas-xi-approved-tiktok-deal-2025-09-19/
2•geox•38m ago•0 comments

I Hate Barney

https://popupbackpacker.com/i-hate-barney/
2•dxs•47m ago•0 comments

Find My Parking Cops

https://walzr.com/sf-parking/
2•walz•50m ago•1 comments

2026 BMW iX3 Revealed: Meet the Blueprint for BMW's Future

https://www.thedrive.com/news/2026-bmw-ix3-revealed-meet-the-blueprint-for-bmws-future
2•PaulHoule•50m ago•0 comments

Anger after pharaoh's gold bracelet stolen from a Cairo museum is melted down

https://apnews.com/article/egypt-antiquities-museum-pharaoh-bracelet-stolen-13e6958fe0be0765a6ee7...
2•wslh•53m ago•0 comments

The Free Software Commons

https://jenniferplusplus.com/the-free-software-commons/
1•Bogdanp•56m ago•1 comments

The biggest problem with Liquid Glass

https://twitter.com/MrrrBaiii/status/1969859801288146977
3•deqian•1h ago•2 comments

Phone gets stolen. Your crypto may be next

https://www.ft.com/content/dfbc9ef7-9832-4845-8ea3-c7021506447e
1•throwaway2037•1h ago•1 comments

The Pattern Collector

https://www.sciencenews.org/article/pattern-collector
1•synxdjruebgfhcl•1h ago•1 comments

Open Library Search: Balancing High-Impact with High-Demand

https://blog.openlibrary.org/2025/09/12/open-library-search-balancing-high-impact-with-high-demand/
1•raybb•1h ago•0 comments

Multiple Kernels on a Single System

https://lwn.net/SubscriberLink/1038847/18f41d9b28065fce/
1•chmaynard•1h ago•1 comments

Token Models as Statistical Simulations: A Different Take

https://medium.com/@thomasquintana/token-models-as-statistical-simulations-a-different-take-02f1e...
1•thomasquintana•1h ago•1 comments

Why Marriage Is Increasingly for the Affluent

https://www.wsj.com/lifestyle/relationships/marriage-wealthy-luxury-class-3f792e67
7•sandwichsphinx•1h ago•2 comments