使用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
那么在这种情况下如何将整数转换为字符串?
答案 0 :(得分:2)
在c.Header()
调用中传递变量的值而不是指针。
c.Header("X-Total-Count", strconv.Itoa(count))
供参考,CI/CD pipeline for an ASP.NET Core Application to deploy to Linux with VSTS是:
func (c *Context) Header(key, value string) {