Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to capture values ​of type int – GO, GIN, POSTFORM

I’m building a simple crud integrated with html. My crud consists of a table of products, in my table I have the following fields:

package models

type Produtos struct {
    IDProd       int    `json:"id_Prod" gorm:"primaryKey"`
    NomeProduto  string `json:"nome_produto"`
    Quantidade   int    `json:"quantidade"`
    PrecoProduto int    `json:"preco_produto"`
}

I’m trying to make a fullstack crud, but I’m stuck in the part where I enter the Quantity and Product_Price values.
My GO code is like this:

package handlers

import (
    "github.com/gin-gonic/gin"
    "github.com/guilherm5/crud/models"
)

func (h Handler) CreateProduct(c *gin.Context) {
    //CREATE IN WEBSITE

    var createHTML = []models.Produtos{{
        NomeProduto:  c.Request.FormValue("nome_produto"),
        Quantidade:   c.Request.FormValue("quantidade"),
        PrecoProduto: c.Request.FormValue("preco_produto")}}

    h.DB.Create(&createHTML)

my html code like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 <section class="container-principal">
        <form action="/create" method="post" enctype="multipart/form-data">

            <div class="title">
                <h1>Crie seu usuario</h1>
            </div>

            <div class="single-input">
                <input placeholder="nome do produto" type="text" id="nome_produto" name="nome_produto">
            </div>

            <div class="single-input">
                <input placeholder="quantidade em estoque" type="number" id="quantidade" name="quantidade">
            </div>

            <div class="single-input">
                <input placeholder="preco do produto" type="number" id="preco_produto" name="preco_produto">
            </div>

            <a class="new" href="/"><button id="botao" type="submit" class="glow-on-hover" type="button">Enviar</button></a>
        </form>

my problem is in the GO code, in my Quantity part it is issuing the following error:

my problem is in the GO code, in my Quantity part: c.Request.FormValue("quantidade"), it is issuing the following error:

cannot use c.Request.FormValue("quantidade") (value of type string) as int value in struct literal

the parameter c.Request.FormValue does not accept int in its entries, how can I solve this? Is there any other parameter to be used? can someone help me?

>Solution :

It looks like the problem is that you are trying to assign a string value to an int field in your Produtos struct. The FormValue function returns a string, so you’ll need to convert it to an integer before you can assign it to the Quantidade field.

Here is an example of what you can do:

import "strconv"

...

quantityString := c.Request.FormValue("quantidade")
quantity, err := strconv.Atoi(quantityString)
if err != nil {
    // handle the error, for example by returning a response to the user
}

var createHTML = []models.Produtos{{
    NomeProduto:  c.Request.FormValue("nome_produto"),
    Quantidade:   quantity,
    PrecoProduto: c.Request.FormValue("preco_produto")}}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading