无法在分配中将电话(字符串类型)用作int类型

时间:2019-03-24 02:41:46

标签: go go-gorm gin

我遇到错误“无法在分配中将电话(类型字符串)用作int类型”, 该如何解决?

我在github.com/gin-gonic/gin和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 个答案:

答案 0 :(得分:1)

PostForm中的值都是字符串。您应将phone声明为字符串类型,或将电话号码从字符串转换为整数。像strconv.Atoistrconv.ParseInt

phone := c.PostForm("phone")
phoneNumber, _ := strconv.Atoi(phone)
em.Phone = phoneNumber