Skip to content

Pagination helper to paginate data like Laravel pagination response

License

Notifications You must be signed in to change notification settings

Digitwires/go-paginate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Reference Go Report Card

go-paginate

go-paginate is a simple Go package that adds pagination functionality to slice of data.

Installation

To install go-paginate, use go get:

go get github.com/digitwires/go-paginate

Usage

The go-paginate package provides a Paginate function that takes in a slice of data, the current page number, and the desired page size. It returns a new slice that contains only the data for the current page, as well as information about the total number of pages and the total number of items.

Here's an example:

package main

import (
	"fmt"

	pageutil "github.com/digitwires/go-paginate"
)

func main() {
	data := []interface{}{"apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew", "kiwi"}
	perPage := 3
	currentPage := 2
	baseUrl := "http://example.com"

	paginate, err := pageutil.Paginate(data, perPage, currentPage, baseUrl)
	if err != nil {
        fmt.Println(err)
    }

	fmt.Println("Current Page:", paginate.CurrentPage)
	fmt.Println("Total Pages:", paginate.Total)
	fmt.Println("Data:", paginate.Data)
	fmt.Println("Previous Page:", paginate.PrevPage)
	fmt.Println("Next Page:", paginate.NextPage)
}

Output:

Current Page: 1
Total Pages: 3
Data: [apple banana cherry]
Previous Page: http://example.com?page=1
Next Page: http://example.com?page=3

License

go-paginate is licensed under the MIT License. See LICENSE for the full license text.

Contributing

Contributions are welcome! If you find a bug or have a feature request, please open an issue on GitHub. If you'd like to contribute code, please fork the repository and submit a pull request.

Note that you can copy and paste this code into a `README.md` file in the root of your repository to use it as your package's documentation.

Author

go-paginate was written by Ahmed M. Ibrahim.

About

Pagination helper to paginate data like Laravel pagination response

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages