How do I paginate results with Gin in Go?

Go, Gin, Pagination, REST API, Golang
This example demonstrates how to paginate results in a RESTful API using the Gin framework in Go.
package main import ( "github.com/gin-gonic/gin" "net/http" "strconv" ) type Item struct { ID int `json:"id"` Name string `json:"name"` } var items = []Item{ {1, "Item 1"}, {2, "Item 2"}, {3, "Item 3"}, {4, "Item 4"}, {5, "Item 5"}, {6, "Item 6"}, {7, "Item 7"}, // Add more items as needed } func main() { router := gin.Default() router.GET("/items", func(c *gin.Context) { pageStr := c.Query("page") limitStr := c.Query("limit") page, err := strconv.Atoi(pageStr) if err != nil || page < 1 { page = 1 } limit, err := strconv.Atoi(limitStr) if err != nil || limit < 1 { limit = 2 // Default limit } start := (page - 1) * limit end := start + limit if start > len(items) { start = len(items) } if end > len(items) { end = len(items) } paginatedItems := items[start:end] c.JSON(http.StatusOK, gin.H{ "page": page, "limit": limit, "items": paginatedItems, }) }) router.Run(":8080") }

Go Gin Pagination REST API Golang