How do I paginate results with Echo in Go?

Paginating results in Echo can be efficiently achieved by handling query parameters for page numbers and limits. The following example illustrates how to implement pagination in a Go application using Echo:

package main import ( "net/http" "strconv" "github.com/labstack/echo/v4" ) func main() { e := echo.New() // Simulated database results items := make([]string, 100) for i := 0; i < 100; i++ { items[i] = "Item " + strconv.Itoa(i+1) } e.GET("/items", func(c echo.Context) error { page, _ := strconv.Atoi(c.QueryParam("page")) limit, _ := strconv.Atoi(c.QueryParam("limit")) if page <= 0 { page = 1 } if limit <= 0 { limit = 10 } start := (page - 1) * limit end := start + limit if end > len(items) { end = len(items) } if start >= len(items) { return c.JSON(http.StatusOK, []string{}) } return c.JSON(http.StatusOK, items[start:end]) }) e.Logger.Fatal(e.Start(":8080")) }

Go Echo Pagination REST API Golang Web Framework JSON Response