Easily parse and create Link headers for pagination.
Easily create link headers for responses to your paginated endpoints.
func handler(w http.ResponseWriter, req http.Request) {
// ...
linkHeader := link.NewHeader(map[string]*url.URL{
"first": firstURL,
"last": lastURL,
// if these are nil then they will not be included in the header
"next": nextURL,
"prev": prevURL,
})
w.WriteHeader("link", linkHeader.String())
// ...
}
Easily parse link headers from external services to paginate through results.
func loadUsers(path string) {
resp, err := http.DefaultClient.Get(path)
linkHeader, err := link.Parse(resp)
if next := linkHeader.Next(); next != nil {
loadUsers(next.URL.String())
}
}
func main() {
loadUsers("http://service.com/users")
}