gpt4 book ai didi

go - 如何理解这个错误? “unsupported destination, should be slice or struct”

转载 作者:行者123 更新时间:2023-12-01 22:16:59 29 4
gpt4 key购买 nike

我有这样的结构:

type Users struct{
ID uint
Profile *Profile
Name string
ProfileID uint
}
type Profile struct{
ID uint
Name string
}

而且我想插入到与User.Profile1相关的配置文件中
 db.First(&user.Profile, user.ProfileID)

在这里我得到这样的错误
unsupported destination, should be slice or struct 

而且我不明白为什么会出现此错误。我知道解决方案是创建Profile的 slice ,但是我只需要一个Profile。

最佳答案

您尚未初始化Profile结构。您必须首先初始化该结构。

user.Profile = &Profile{}
db.First(user.Profile, user.ProfileID)

关于go - 如何理解这个错误? “unsupported destination, should be slice or struct”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59065456/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com