Home  >  Article  >  Backend Development  >  ## 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?

DDD
DDDOriginal
2024-10-25 13:19:02219browse

## 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:

<code class="go">type Res struct {
    ID int
    Number int
    UserID int
}</code>

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

<code class="go">type res struct {
    id int      `gorm:"column:id"`
    number int  `gorm:"column:number"`
    user_id int `gorm:"column:user_id"`
}</code>

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

The above is the detailed content of ## Why are my Go-GORM struct fields returning default values despite a successful query?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn