search
HomeBackend DevelopmentGolangGolang GIN error when binding form data

绑定表单数据时Golang GIN出错

php editor Xinyi brings you a solution to the problem of errors when binding form data in the Golang GIN framework. When using the GIN framework for form data binding, you sometimes encounter some problems, such as the inability to parse the form data correctly. These problems may be caused by parameter binding, data type mismatch, etc. This article will introduce how to correctly bind form data and solve common errors to help developers successfully use the GIN framework for development work.

Question content

When I try to bind a form data request to a structure, it errors out with "Fatal Error: Stack Overflow".

This is my code. There's nothing to explain. I'm getting started with the code but can't figure it out.

Structure

type Wish struct {
    ID                int                `gorm:"primarykey;autoIncrement" json:"id"`
    CreatedAt         time.Time          `json:"created_at"`
    UpdatedAt         time.Time          `json:"updated_at"`
    DeletedAt         gorm.DeletedAt     `gorm:"index" json:"deleted_at"`
    UserID            int                `json:"user_id" form:"user_id"`
    User              *User              `gorm:"foreignKey:UserID" json:"user_data,omitempty"`
    WishTypeID        int                `json:"wish_type_id" form:"wish_type_id"`
    WishType          *WishType          `gorm:"foreignKey:WishTypeID" json:"wish_type_data,omitempty"`
    ProcessTrack      []*ProcessTrack    `gorm:"foreignKey:WishID" json:"process_track,omitempty"`
    VacationDateRange *VacationDateRange `gorm:"foreignKey:WishID" json:"vacation_date_range,omitempty"`
    Content           string             `gorm:"type:varchar(255)" json:"content" form:"content"`
    Status            WishStatus         `gorm:"type:integer" json:"status" form:"status"`
    Files             []*File            `gorm:"polymorphic:Module;polymorphicValue:wish_files" json:"files,omitempty"`
}

Controller

var wish migrations.Wish
    if err := c.Bind(&wish); err != nil {
        c.JSON(400, gin.H{"error": err.Error(), "message": "Talep Okunamadı!"})
        return
    }
    c.JSON(200, wish)
    return

Request

Solution

I modified the controller

type Req struct {
        Content           string                        `form:"content"`
        WishTypeID        int                           `form:"wish_type_id"`
        VacationDateRange *migrations.VacationDateRange `form:"vacation_date_range"`
    }
    err, i, g := authorizer.AuthorizeIt(c, a.Subject, a.Action)
    if err != nil {
        c.JSON(i, g)
        return
    }
    var wishReq Req
    var wish migrations.Wish
    if err := c.Bind(&wishReq); err != nil {
        c.JSON(400, gin.H{"error": err.Error(), "message": "Wish can't bind."})
        return
    }
    wish.WishTypeID = wishReq.WishTypeID
    wish.Content = wishReq.Content
    wish.VacationDateRange = wishReq.VacationDateRange

But I still don't understand why it can't be the first style. I've also added common usage. It usually works too.

err, i, g := authorizer.AuthorizeIt(c, a.Subject, a.Action)
    if err != nil {
        c.JSON(i, g)
        return
    }
    var announce mig.Announce

    err = c.Bind(&announce)
    if err != nil {
        c.JSON(400, gin.H{"error": err.Error(), "message": "Announce can't bind. Error Code: AN-CRT-20"})
        return
    }

The above is the detailed content of Golang GIN error when binding form data. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete
Spring Security 6:cors() 已弃用并标记为删除Spring Security 6:cors() 已弃用并标记为删除Feb 10, 2024 pm 11:45 PM

我有下面的代码:publicSecurityFilterChainsecurityFilterChain(HttpSecurityhttp)throwsException{returnhttp.httpBasic().disable().cors().and().csrf().disable().authorizeHttpRequests().requestMatchers("

C++ lambda 表达式是否支持递归?C++ lambda 表达式是否支持递归?Apr 17, 2024 pm 09:06 PM

是的,C++Lambda表达式可以通过使用std::function支持递归:使用std::function捕获Lambda表达式的引用。通过捕获的引用,Lambda表达式可以递归调用自身。

使用 ucontext 的 Golang+CGO 在使用不同堆栈时因 SIGSEGV 或 SIGTRAP 崩溃(故意)崩溃使用 ucontext 的 Golang+CGO 在使用不同堆栈时因 SIGSEGV 或 SIGTRAP 崩溃(故意)崩溃Feb 09, 2024 pm 12:15 PM

我目前正在编写Golang+CGO程序,并将在CGO中使用posixucontext。由于我所有的核心逻辑都将在ucontext的bind函数中,所以我们应该捕获所有错误的代码。我通过访问空指针来测试它,这给了我完全不同的行为,所有这些行为都取决于ucontext使用的堆栈位置。以下是带有简化示例的更多详细信息。如果我在线程的堆栈上分配ucontext堆栈,它将触发SIGSEGV。但如果我在堆上分配它,它会首先触发SIGSEGV,然后在调用morestack_noctxt时触发SIGT

c++开始执行为什么会闪退c++开始执行为什么会闪退Apr 22, 2024 pm 05:57 PM

C++ 程序启动时闪退的原因包括:缺少必需库或依赖项未初始化指针或引用堆栈溢出段错误操作系统配置问题程序错误硬件问题

如何解决C++运行时错误:'stack overflow'?如何解决C++运行时错误:'stack overflow'?Aug 25, 2023 pm 10:00 PM

如何解决C++运行时错误:'stackoverflow'在C++程序中,当递归层数过深或者程序使用的内存超出栈的容量会导致运行时错误"stackoverflow"。这种错误发生时,程序会崩溃,并且很难找出具体的原因。本文将介绍一些解决'stackoverflow'错误的方法,并提供一些代码示例。运行时错误"stackoverflow"的主要原因是栈内

C++ 函数的递归实现:递归与非递归算法的比较分析?C++ 函数的递归实现:递归与非递归算法的比较分析?Apr 22, 2024 pm 03:18 PM

递归算法通过函数自调用解决结构化的问题,优点是简洁易懂,缺点是效率较低且可能发生堆栈溢出;非递归算法通过显式管理堆栈数据结构避免递归,优点是效率更高且避免堆栈溢出,缺点是代码可能更复杂。选择递归或非递归取决于问题和实现的具体限制。

C++ 函数对程序性能有哪些影响?C++ 函数对程序性能有哪些影响?Apr 12, 2024 am 09:39 AM

函数对C++程序性能的影响包括函数调用开销、局部变量和对象分配开销:函数调用开销:包括堆栈帧分配、参数传递和控制权转移,对小函数影响显著。局部变量和对象分配开销:大量局部变量或对象创建和销毁会导致堆栈溢出和性能下降。

Java函数与Haskell函数的区别?Java函数与Haskell函数的区别?Apr 23, 2024 pm 09:18 PM

Java和Haskell函数的主要区别在于:语法:Java使用return关键字返回结果,而Haskell使用赋值符号(=)。执行模型:Java采用顺序执行,而Haskell采用懒惰求值。类型系统:Java具有静态类型系统,而Haskell具有强大的灵活类型系统,可在编译时和运行时检查类型。实战性能:Haskell在处理大输入时比Java更有效,因为它使用尾递归,而Java使用递归。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!