您应该看一下bson包的内联标志(记录在bson.Marshal下)。它应该允许您执行以下操作:
type adminUser struct { User `bson:",inline"` Secret string `json:"secret,omitempty" bson:"secret,omitempty"`}不过,现在你会发现,你得到重复键错误,当您试图从这种结构的数据库中读取,因为两者
adminUser和
User包含的关键
secret。
在您的情况下,我将从中删除该
Secret字段,
User
而只保留其中的一个
adminUser。然后,每当需要写入
secret字段时,请确保使用
adminUser。



