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....
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/16e7b06e-0b22-4997-bf1e-8d9b251f3c23n%40googlegroups.com.