-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)
}
David Buck
  • 3,752
  • 35
  • 31
  • 35
rsmnarts
  • 185
  • 2
  • 14
  • 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? – Ken White Mar 24 '19 at 02:55

1 Answers1

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
HolaYang
  • 419
  • 2
  • 10