Skip to content

spec: direct reference to embedded fields in struct literals #9859

Closed

Description

@adg

(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).

Can we allow it?

(cc @bradfitz)

Activity

  1. adg commented on Feb 12, 2015

    @adg
    ContributorAuthor

    One possible argument against is that it allows you to effectively specify the same field twice, for example:

    T{E: E{A: 1}, A: 2}
    

    But this case already exists

    S{B: 2, B: 3}
    

    and is disallowed, so maybe that isn't worth worrying about.

  2. bradfitz commented on Feb 12, 2015

    @bradfitz
    Contributor

    FWIW, @adg and I both tripped over this independently. We both assumed T{A: 1} would work.

  3. griesemer commented on Feb 12, 2015

    @griesemer
    Contributor

    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.

  4. self-assigned this
    on Feb 12, 2015
  5. dominikh commented on Feb 13, 2015

    @dominikh
    Member

    Is there any known reason it wasn't considered back in #164?

  6. griesemer commented on Feb 13, 2015

    @griesemer
    Contributor

    @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.

  7. added
    LanguageChange
    on Feb 13, 2015
  8. josharian commented on Feb 18, 2015

    @josharian
    Contributor

    r's comment on this on golang-nuts was:

    It might one day, but as it stands the requirement to provide more information is more robust against changes in the data types.

  9. added this to the Unplanned milestone on Apr 10, 2015
  10. 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
  11. added
    v2
    on Jun 20, 2017
  12. 111 remaining items

  13. added a commit that references this issue on May 7, 2026
  14. gopherbot commented on May 21, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/780220 mentions this issue: cmd/compile: compute embedded field offset in static initialization

  15. gopherbot commented on May 26, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/783400 mentions this issue: doc: document new struct literal field selectors (update release notes)

  16. gopherbot commented on Jun 2, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/786422 mentions this issue: gopls/internal/analysis/fillstruct: skip promoted field before go127

  17. gopherbot commented on Jun 4, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/787041 mentions this issue: cmd/compile: better error message when using promoted field with version < 1.27

  18. justinhwang commented on Jun 7, 2026

    @justinhwang

    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)

  19. adonovan commented on Jun 8, 2026

    @adonovan
    Member

    @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.

  20. abdamah commented on Sep 21, 2026

    @abdamah

    Thanks , this cleans verbose embedded structs while still some confusing for same field names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

LanguageChangeLanguageChangeReviewProposalProposal-Accepted

Type

No type

Projects

  • Status
    Accepted

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions