Home > Net >  Undeclared Name const when using RAW Built-in function GORM
Undeclared Name const when using RAW Built-in function GORM

Time:08-24

I want to use Raw function in GORM as requirements on my personal project

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

const (RetrieveData = `SELECT id, name, address FROM users WHERE id = ?`)

And then I build 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

undeclared name: RetrieveData (compile)go-staticcheck

Is it because untype string type?

CodePudding user response:

use method find()

err := DB.Raw("[sql]").Find(&data).Error

CodePudding user response:

Most probably RetrieveData is not accessible in func RetrieveUserData
Either move it to same package or use <package_name>.RetrieveData

  • Related