Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: encoding/json: add ... json tag to spread #52138

maru44 opened this issue Apr 4, 2022 · 4 comments

proposal: encoding/json: add ... json tag to spread #52138

maru44 opened this issue Apr 4, 2022 · 4 comments


Copy link

maru44 commented Apr 4, 2022

I added new json tag.
This can be instead of embedded generics.

As you know, we cannot embed generics to struct.

this is invalid

type bad[T any] struct{
    Name string

I want use embed in json, so I added new json tag to spread struct in its parent.


type good[T any] struct {
    Name string
    Free T `json:"..."`

type more struct {
    Age int
    Country string

var foo = good[more]{
    Name: "Foo",
    more: more{
        Age: 20,
        Country: "U.S.A",

If you encode it in json. It'll be like this.

{"Name": "Foo", "Age": 20, "Country": "U.S.A"}

My PR.

@maru44 maru44 added the Proposal label Apr 4, 2022
@gopherbot gopherbot added this to the Proposal milestone Apr 4, 2022
Copy link

icholy commented Apr 4, 2022

The door isn't closed on allowing embedding type parameters #43621

Copy link

DeedleFake commented Apr 4, 2022

Anonymous fields can also be manually named, overriding their typical behavior. I think having a way to do the opposite and treat a named field as though it were anonymous makes sense. Even without generics, it would allow you to have a field of an interface type and spread the values to the parent object:

type Example {
  ID int `json:"id"`
  Name string `json:"name"`
  Extra any `json:"..."`

Copy link

dsnet commented Apr 4, 2022

This sounds like a duplicate of #6213.

Copy link

Closing as dup. Please comment if you disagree.

@golang golang locked and limited conversation to collaborators Apr 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet

No branches or pull requests

6 participants