使用 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) {
关于go - 在这种情况下,如何使用 go 将整数更改为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50902074/