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

DDD
Release: 2024-10-25 13:19:02
Original
230 people have browsed it

## 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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!