在golang中嵌入结构会产生错误“未知字段”。 [英] Embedding structs in golang gives error "unknown field"
问题描述
我在用户
程序包中有一个结构
,名为 account
i have a struct
in user
package called account
type Account struct {
Tp string `json:"type"bson:"type"`
AccountId string `json:"account_id"bson:"account_id"`
Credentials map[string]interface{} `json:"credentials,omitempty"bson:"credentials,omitempty"`
ProfilePicture string `json:"profile_picture,omitempty"`
Username string `json:"username"bson:"username"`
AccessToken map[string]interface{}`bson:"access_token,omitempty"`
}
和用户/帐户
im尝试将此帐户结构嵌入另一个结构中
and in user/accounts
im trying to embed this account struct into another struct
type returnAccount struct {
user.Account
AccessToken string `json:"access_token,omitempty"`
}
用户包在尝试之前已正确导入嵌入我正在成功使用它
user package is properly imported before trying to embed i was using it successfully
最终在循环中我得到了输入用户帐户并制作 returnAccount
的地图,然后从我的函数
返回,这是我的函数
finaly in a loop i am getting user accounts and making a map of returnAccount
and returning from my function
here is my function
func getAccounts(usr *user.AuthenticatedUser, id ...string) (accounts map[string]returnAccount) {
accounts = make(map[string]returnAccount)
if len(id) > 0 {
for _, v := range id {
for _, acnt := range usr.Accounts {
if acnt.AccountId == v {
accounts[acnt.AccountId] = returnAccount{
Tp: acnt.Tp,
AccountId:acnt.AccountId,
}
}
}
}
return
}
for _, v := range usr.Accounts {
accounts[v.AccountId] = returnAccount{
Tp: v.Tp,
AccountId:v.AccountId,
Username: v.Username,
}
}
return
}
但是此代码不会在此处编译,错误消息
However this code wont compile here is the error message
# sgin/api/user/accounts
api/user/accounts/getaccounts.go:16: unknown returnAccount field 'Tp' in struct literal
api/user/accounts/getaccounts.go:17: unknown returnAccount field 'AccountId' in struct literal
api/user/accounts/getaccounts.go:26: unknown returnAccount field 'Tp' in struct literal
api/user/accounts/getaccounts.go:27: unknown returnAccount field 'AccountId' in struct literal
api/user/accounts/getaccounts.go:28: unknown returnAccount field 'Username' in struct literal
$ b中的未知returnAccount字段'Username'
$ b
一切似乎都非常简单明了,我无法弄清楚为什么会出现此错误,我需要达到Account结构的所有成员都已导出
everything seems pretty straightforward and simple i cannot figure out why i get this error all members i need to reach of the Account struct are exported
我需要此字段的原因是我想通过api发送访问令牌给客户端,而不是秘密,并且我想降低缩进级别
The reason why i need this field is i want to send access token to clients through api but not the secret and also i want to reduce the indention level
推荐答案
您正在尝试初始化提升字段,这是复合文字无法实现的。来自查看规范:
You are trying to initialize promoted fields which is not possible by composite literals. From Go spec:
如果 xf 是表示该字段或方法f的合法选择器,则结构x中匿名字段的字段或方法f被称为升级。
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
这篇关于在golang中嵌入结构会产生错误“未知字段”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!