Embedding structs in golang gives error "unknown field"
You can't initialize the fields in the embedded type directly, but you can do it like this:
accounts[v.AccountId] = returnAccount{
Account: Account{
Tp: v.Tp,
AccountId: v.AccountId,
Username: v.Username,
},
}
Or, if v
is of type Account
, you can just use
accounts[v.AccountId] = returnAccount{
Account: v,
}
You are trying to initialize promoted fields which is not possible by composite literals. From Go spec:
A field or method f of an anonymous field in a struct x is called promoted if x.f is a legal selector that denotes that field or method f.
Promoted fields act like ordinary fields of a struct except that they cannot be used as field names in composite literals of the struct.
But you can access them using dot notation:
ra:= returnAccount{}
ra.Tp = acnt.Tp