You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
spec: direct reference to embedded fields in struct literals #9859
(Status: referred from LanguageChange to Proposal. --adonovan)
Consider
type E struct {
A int
}
type T struct {
E
}
This works:
T{E: E{A: 1}}
This does not:
T{A: 1}
Makes some struct literals more verbose than they need be, and makes them asymmetrical to their usage (where you can access the embedded struct's fields directly).
Reacted by Kirill Smelkov, DeedleFake, Joe Tsai, Cezar Sá Espinola, Kaveh Ghasemloo, Roxy Light, jimmyfrasche, Roberto Clapis, Christian Pena Valerio, k5.trismegistus and 201 more
Reacted by Matt Mc and davidd-dojo
Reacted by Ewen Quimerc'h, Barkin Simsek, Eduard Castany, David Gubler, Benjamin Guest, Gwenn Le Bihan, takanuva15, Robert Findley, saffage, Jonathan Hall and 5 more
@dominikh issue #164 was about a "programmer error" or misunderstanding of the spec as written.
Usually we don't look at each such error and consider a spec change. However this has come up before (it has restricted what I wanted to do in APIs significantly) so maybe it's time to at least investigate the consequences of such a language change more thoroughly.
That said, language changes are really very low priority. We'll get to it when we get to it.
changed the title [-]spec: direct reference to embedded fields in struct literals[/-][+]proposal: spec: direct reference to embedded fields in struct literals[/+]on Jun 20, 2017
Embedded struct name having a collision with a field name is a bit confusing (albeit already confusing today)
type C struct {
B string
C string
}
type B struct {
C
}
type A struct {
B
}
Today we:
a := A{B: B{C: C{B: "b", C: "c"}}} // must be verbose
a.B = "b2" // can't do this
a.C = "c2" // can't do this
a.C = C{B: "b2", C: "c2"} // can do this
which means we'd be able to do a := A{C: C{B: "b", C: "c"}} but not a := A{B: "b", C: "c"}
Either way people already have to be verbose in this case (and would face compile issues if they tried to do the latter) so nothing breaks but just wanted to flag (and was trying to think of a case where you might embed an external struct, where they later add in a field that collides with an embedded struct somewhere in the chain)
@justinhwang Embedded struct name having a collision with a field name is a bit confusing
Yes, the rule for struct literals is similar to the rule for assignments: the addition of an F field may cause x.F = 1 to break; now it may also cause T{F: 1} to break. This was considered during the review (see #9859 (comment) et seq) and ultimately deemed acceptable.
Activity
adg commentedon Feb 12, 2015
One possible argument against is that it allows you to effectively specify the same field twice, for example:
But this case already exists
and is disallowed, so maybe that isn't worth worrying about.
bradfitz commentedon Feb 12, 2015
FWIW, @adg and I both tripped over this independently. We both assumed
T{A: 1}would work.griesemer commentedon Feb 12, 2015
I've wished to be to able to do this many times (go/ast is a prime example).
I don't know that we ever thought through all the consequences, but it's perhaps worthwhile considering.
dominikh commentedon Feb 13, 2015
Is there any known reason it wasn't considered back in #164?
griesemer commentedon Feb 13, 2015
@dominikh issue #164 was about a "programmer error" or misunderstanding of the spec as written.
Usually we don't look at each such error and consider a spec change. However this has come up before (it has restricted what I wanted to do in APIs significantly) so maybe it's time to at least investigate the consequences of such a language change more thoroughly.
That said, language changes are really very low priority. We'll get to it when we get to it.
josharian commentedon Feb 18, 2015
r's comment on this on golang-nuts was:
net/url: add RawPath field, a hint at the desired encoding of Path
[-]spec: direct reference to embedded fields in struct literals[/-][+]proposal: spec: direct reference to embedded fields in struct literals[/+]111 remaining items
all: update golang.org/x dependencies
cmd/compile: compute embedded field offset in static initialization
gopherbot commentedon May 21, 2026
Change https://go.dev/cl/780220 mentions this issue:
cmd/compile: compute embedded field offset in static initializationgopherbot commentedon May 26, 2026
Change https://go.dev/cl/783400 mentions this issue:
doc: document new struct literal field selectors (update release notes)doc: document new struct literal field selectors (update release notes)
gopherbot commentedon Jun 2, 2026
Change https://go.dev/cl/786422 mentions this issue:
gopls/internal/analysis/fillstruct: skip promoted field before go127gopherbot commentedon Jun 4, 2026
Change https://go.dev/cl/787041 mentions this issue:
cmd/compile: better error message when using promoted field with version < 1.27cmd/compile: better error message when using promoted field with vers…
justinhwang commentedon Jun 7, 2026
Embedded struct name having a collision with a field name is a bit confusing (albeit already confusing today)
Today we:
which means we'd be able to do
a := A{C: C{B: "b", C: "c"}}but nota := A{B: "b", C: "c"}Either way people already have to be verbose in this case (and would face compile issues if they tried to do the latter) so nothing breaks but just wanted to flag (and was trying to think of a case where you might embed an external struct, where they later add in a field that collides with an embedded struct somewhere in the chain)
adonovan commentedon Jun 8, 2026
Yes, the rule for struct literals is similar to the rule for assignments: the addition of an F field may cause
x.F = 1to break; now it may also causeT{F: 1}to break. This was considered during the review (see #9859 (comment) et seq) and ultimately deemed acceptable.ci(lint): golangci-lint 2.12.2 -> 2.13.2, which can parse the Go 1.27…
ci(lint): golangci-lint 2.12.2 -> 2.13.2, which can parse the Go 1.27…
ci: bump golangci-lint to v2.13.2 for Go 1.27 support
!17336 merge bump-staticcheck-2026.2 into main
abdamah commentedon Sep 21, 2026
Thanks , this cleans verbose embedded structs while still some confusing for same field names.