go - 在这种情况下,如何使用 go 将整数更改为字符串?

标签 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

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

最佳答案

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/

相关文章:

methods - Go嵌入结构调用子方法而不是父方法

go - 获取对象返回空字符串作为值Golang

sql - 如何在gorm中创建双向多对多关系

Gorm Scan 未绑定(bind)到结构

mysql - 我不明白那些 '' 字段在数据库结构中的含义,它的目的是什么

Golang 在运行 main.go 时不断崩溃,异常 0xc0000005

pointers - 为什么Go允许将接口(interface)变量分配给未实现值接收者的值?

go - Couchbase gocb 批量操作提供部分为空的结果

go - 如何将变量(不是结构成员)传递到 text/html 模板中。戈朗

postgresql - Go-Gorm 是否支持在 PostgreSQL 中使用自动递增 ID 插入分区表?