首页 > 解决方案 > 不能在赋值中使用电话(类型字符串)作为 int 类型

问题描述

我有一个错误“不能在赋值中使用电话(类型字符串)作为类型 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)
}

标签: gogo-gormgo-gin

解决方案


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

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

推荐阅读