在这种情况下,如何用go将整数更改为字符串?
问题描述:
Using gorm
to connect db. Here get all the records:
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)
}
}
About count
, the c.Header("X-Total-Count", &count)
can't passed since this error:
cannot use &count (type *int) as type string in argument to c.Header
Have tried strconv.Itoa(&count)
, got another error:
cannot use &count (type *int) as type int in argument to strconv.Itoa
So how to convert integer to string in this case?
答
In the c.Header()
call pass the value of the variable instead of a pointer.
c.Header("X-Total-Count", strconv.Itoa(count))
For reference, the method signature is:
func (c *Context) Header(key, value string) {