Paginating through API response

I have an api response that I have to paginate through:

func getAllOrgs() []int {
	var allOrgIds []int
	var listOfAllOrganizations Orgs
	var x = 1
	for x == 1 {
		if listOfAllOrganizations.Nextpage != nil {
                 DO CODE HERE
}

The structure of the URL for the next page is http://someurl.com?page=PAGENUBMER

When there are no more pages to generate that link in the api response, next_page looks like next_page:null.

How can I compare nil to null? I’ve tried setting the Nextpage to *string and i’ve also tried setting listofallorges.nextpage != ""

But it’s never evaluated.

There’s not enough code here to know what’s going on. Based on how you wrote “next_page:null”, I’m guessing that the API probably returns JSON and that your Orgs struct might have a field like this in it:

type Orgs struct {
    // ???
    Nextpage *string `json:"next_page"`
    // ???
}

And I’m also guessing that you’re deserializing JSON somewhere in (or outside of?) that loop, too. However, because all the important stuff is happening somewhere else, there’s not much help we can give.

1 Like

Hey @skillian,

You are correct, I did figure it out though. My struct was

type Orgs struct {
    // ???
    Nextpage string `json:"next_page"`
    // ???
}

instead of setting Nextpage as a pointer. Thanks though!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.