我遇到错误“无法在分配中将电话(类型字符串)用作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)
}
答案 0 :(得分:1)
PostForm
中的值都是字符串。您应将phone
声明为字符串类型,或将电话号码从字符串转换为整数。像strconv.Atoi
或strconv.ParseInt
phone := c.PostForm("phone")
phoneNumber, _ := strconv.Atoi(phone)
em.Phone = phoneNumber