"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > ## Why are my Go-GORM struct fields returning default values despite a successful query?

## Why are my Go-GORM struct fields returning default values despite a successful query?

Published on 2024-11-17
Browse:568

## Why are my Go-GORM struct fields returning default values despite a successful query?

Accessing Query Results in Go-GORM Structures

You're facing an issue where the result of a query into a 'res' structure remains default values despite a successful query execution. This is related to naming conventions in Go-GORM.

To address this, you can either make your 'res' type publicly accessible with public fields:

type Res struct {
    ID int
    Number int
    UserID int
}

Alternatively, you can specify mappings between database columns and struct fields:

type res struct {
    id int      `gorm:"column:id"`
    number int  `gorm:"column:number"`
    user_id int `gorm:"column:user_id"`
}

These adjustments will ensure proper field mapping and return accurate results from your query.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3