If I have a struct
type User struct {
Name string
ID int
}
type Group struct {
Leader User
}
Why is it that the go tool can't infer what type I'm constructing for the Leader field here?
g := Group{
Leader: {
Name: "Jamie",
ID: 5,
},
}
Cleary, the compiler knows I'm constructing a variable of type Group, and thus should know that Leader: is a field in Group, and thus what type I am constructing to assign to that field. It's not like it's an interface where I could be assigning anything.
Finally, why does this work with maps, but not struct fields?
users := map[string]User {
"Jamie" : {
Name: "Jamie",
ID: 5,
},
}
This
works and feels almost identical to the Group code above. Define the enclosing type, and then the subtype is obvious and thus implicit.
I presume this is some parser thing where it can't distinguish between two possible valid constructions, though I can't think of exactly what other construction it might mean....