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: avoid redefining the struct content in literal composite #63571

Closed
metal3d opened this issue Oct 16, 2023 · 3 comments
Closed

proposal: avoid redefining the struct content in literal composite #63571

metal3d opened this issue Oct 16, 2023 · 3 comments

Comments

@metal3d
Copy link

metal3d commented Oct 16, 2023

Hello,

I often need to create composed types to parse structured data. And I wonder if we could avoid redefining the content of the struct that we declare inside another.

For example:

https://goplay.tools/snippet/DLsNoPTpvh2

The struct is like this:

type Result struct {
    Name string `json:"name"`
    Data []struct {
        Title  string `json:"title"`
        Author string `json:"author"`
    } `json:"data"`
}

Parsing a JSON file with this is OK. But, if I want to create a document with this, I need to write:

r := &Result{
   Name: "Foo",
   Data: struct { // we already defined the struct content...
        Title  string `json:"title"`
        Author string `json:"author"`
   }{
         Title:  "test",
         Author: "me",
    },
}

It's a pity to not be able to write it like this:

r := &Result {
   Name: "Foo",
   Data: {
        Title:  "test",
        Author: "me",
    },
}

Of course, I understand that if "Data" is a slice of structs, it needs a better syntax.

That's just an idea.

@gopherbot gopherbot added this to the Proposal milestone Oct 16, 2023
@seankhliao
Copy link
Member

Duplicate of #35304

@seankhliao seankhliao marked this as a duplicate of #35304 Oct 16, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2023
@metal3d
Copy link
Author

metal3d commented Oct 17, 2023

@seankhliao I'm sorry, but I don't see in what it's the same topic as #35304 - what I suggest here is not about anonymous struct, but about typed struct declared in another type that needs to be redeclared in assignation.

Is there anything I badly explained? (it's possible, English isn't my mother thong)

@seankhliao
Copy link
Member

then #21496

@golang golang locked and limited conversation to collaborators Oct 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants