在这种情况下,如何使用 go 将整数更改为字符串?
How to change integer to string with go in this case?
使用gorm
连接数据库。这里获取所有记录:
func GetPeople(c *gin.Context) {
var people []Person
var count int
find_people := db.Find(&people)
find_people.Count(&count)
if err := find_people.Error; err != nil {
c.AbortWithStatus(404)
fmt.Println(err)
} else {
c.Header("X-Total-Count", &count)
c.JSON(200, people)
}
}
关于count
,c.Header("X-Total-Count", &count)
无法通过,因为这个错误:
cannot use &count (type *int) as type string in argument to c.Header
已尝试 strconv.Itoa(&count)
,出现另一个错误:
cannot use &count (type *int) as type int in argument to strconv.Itoa
那么在这种情况下如何将整数转换为字符串?
在 c.Header()
调用中传递变量值而不是指针。
c.Header("X-Total-Count", strconv.Itoa(count))
作为参考,the method signature是:
func (c *Context) Header(key, value string) {
使用gorm
连接数据库。这里获取所有记录:
func GetPeople(c *gin.Context) {
var people []Person
var count int
find_people := db.Find(&people)
find_people.Count(&count)
if err := find_people.Error; err != nil {
c.AbortWithStatus(404)
fmt.Println(err)
} else {
c.Header("X-Total-Count", &count)
c.JSON(200, people)
}
}
关于count
,c.Header("X-Total-Count", &count)
无法通过,因为这个错误:
cannot use &count (type *int) as type string in argument to c.Header
已尝试 strconv.Itoa(&count)
,出现另一个错误:
cannot use &count (type *int) as type int in argument to strconv.Itoa
那么在这种情况下如何将整数转换为字符串?
在 c.Header()
调用中传递变量值而不是指针。
c.Header("X-Total-Count", strconv.Itoa(count))
作为参考,the method signature是:
func (c *Context) Header(key, value string) {