简体   繁体   English

使用 RAW 内置 function GORM 时未声明的名称 const

[英]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很可能RetrieveDatafunc 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.

相关问题 如何使用Serilog记录器有效禁用Azure Function(隔离模式)中的内置日志? - How to effectively disable built-in logs in Azure Function (isolated mode) using Serilog logger? 如何在 gorm 中使用原始 sql 构建器获取更新的 output - How to get updated output using raw sql builder in gorm Golang:在 function 中使用`const`/`var` 时如何分配 memory? - Golang: How is memory allocated when using `const`/`var` vs in a function? GORM 原始 sql 未执行 - GORM Raw sql not getting executed 使用 Gorm 更新为 0 值 - Update with 0 value using Gorm 在 Golang 中使用 GORM 进行多次更新 - Multiple update using GORM in Golang Azure 物联网边缘/内置指标/代理 - Azure iot edge / built-in metrics / proxy 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义...问题 - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined... issue 使用 GORM 的模式不支持的关系 - Unsupported relations for schema using GORM Azure function 部署失败:“上传内置内容时 SCM_RUN_FROM_PACKAGE 格式错误” - Azure function deployment failed: "Malformed SCM_RUN_FROM_PACKAGE when uploading built content"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM