[英]Undeclared Name const when using RAW Built-in function GORM
I want to use Raw function in GORM as requirements on my personal project我想在 GORM 中使用 Raw function 作为我个人项目的要求
This is my entity这是我的实体
type Student struct {
ID int `json:"id"`
Name string `json:"name"`
Address string `json:"address"`
}
so I create constant to be called in RAW param所以我创建了要在 RAW 参数中调用的常量
const (RetrieveData = `SELECT id, name, address FROM users WHERE id = ?`)
And then I build function然后我建立 function
type mysqlRepository struct {
GormDb *gorm.DB
}
func(repo *mysqlRepository) RetrieveUserData(id int) (Student, error) {
data := Student{}
db := db.GormDb.Raw(RetrieveData, 3).Scan(&data)
return data, db.Error
}
Why do I get warning on my function为什么我的 function 会收到警告
undeclared name: RetrieveData (compile)go-staticcheck
Is it because untype string type?是因为 untype 字符串类型吗?
Most probably RetrieveData
is not accessible in func RetrieveUserData
很可能
RetrieveData
在func RetrieveUserData
中不可访问
Either move it to same package or use <package_name>.RetrieveData
将其移动到相同的 package 或使用
<package_name>.RetrieveData
use method find()使用方法 find()
err := DB.Raw("[sql]").Find(&data).Error
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.