go - 不能在赋值中使用电话(类型字符串)作为 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)
}
解决方案
中的值PostForm
都是字符串。您应该声明phone
为字符串类型,或将电话号码从字符串转换为整数。喜欢strconv.Atoi
或strconv.ParseInt
phone := c.PostForm("phone")
phoneNumber, _ := strconv.Atoi(phone)
em.Phone = phoneNumber
推荐阅读
- java - 在 Squarespace 中显示/隐藏表单域
- r - 使用分页数据框构建 R 包小插曲
- git - 在不影响功能分支的情况下删除主分支中已从功能分支合并的文件
- javascript - 如何在 Svelte 中使用 LeaderLine?
- flutter - 如何获取列表
来自 Flutter 的火力基地 - soap - 如何仅在父标记上应用命名空间限定符
- search - 在列中搜索多个子字符串的可能性:Google 表格
- sql - 使用 count(*) 创建一个组,但如果没有连接则返回 0
- docker - 在 docker 容器内创建的 dir 的符号链接
- javascript - 在 MongoDB Atlas Search 中搜索多个查询/路径