-5

I have an error "cannot use phone (type string) as type int in assignment", how to fix this?

I use in github.com/gin-gonic/gin and github.com/jinzhu/gor

package main

import (
    "github.com/jinzhu/gorm"
    "github.com/gin-gonic/gin"
)

type Employees struct {
    gorm.Model
    Phone int
}

func (idb *InDB) CreateEmployees(c *gin.Context) {
    var (
        em models.Employees
        result gin.H
  )

  phone := c.PostForm("phone")
  em.Phone = phone

  result = gin.H {
        "result": em,
    }
    c.JSON(http.StatusOK, result)
}
1
  • A phone number is not an integer. Why are you declaring it as one? How is (555) 555-5555 an integer value? You can do math operations on an int. How do you do that with a phone number? How do you multiply a phone number by 2? Commented Mar 24, 2019 at 2:55

1 Answer 1

3

Value in PostForm are all strings. You should declare phone as string type, or convert the phone number from string to integer. Like strconv.Atoi or strconv.ParseInt

phone := c.PostForm("phone")
phoneNumber, _ := strconv.Atoi(phone)
em.Phone = phoneNumber
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.