现在,我找到了一个解决方案-将 Scanner
实施为
GoogleAccount。在输入
Scan方法时
[]uint8,我将其转换为字符串并最终进行解析。这个字符串(保存在db中)看起来像
(x,x)-其中
x-是值。当然,这不是实现我目标的正确方法。但是我找不到其他解决方案。
我 强烈建议 按关系使用经典绑定,或者简单地将表中的这些字段保留为最简单的值(而不是作为对象)。
但是,如果您想在表中尝试嵌套对象,可以看看我的实现,也许对您有用:
type Client struct { // many others fields Google GoogleAccount `json:"google"`}type GoogleAccount struct { Id uint64 `json:"id"` Token string `json:"token"`}func (google GoogleAccount) Value() (driver.Value, error) { return "(" + strconv.FormatUint(google.Id, 10) + "," + google.Token + ")", nil}func (google *GoogleAccount) Scan(value interface{}) error { values := utils.GetValuesFromObject(value) google.Id, _ = strconv.ParseUint(values[0], 10, 64) google.Token = values[1] return nil}


