在这种情况下如何将整数更改为字符串?

时间:2018-06-18 02:37:20

标签: go go-gorm

使用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)
         }
  }

关于countc.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

那么在这种情况下如何将整数转换为字符串?

1 个答案:

答案 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) {