Home > Back-end >  Need to filter some data using go and go-gihub, stuck on next steps with the response
Need to filter some data using go and go-gihub, stuck on next steps with the response

Time:12-10

I’m using go with the go-gihub library and managed to list some releases from an example repo shown in the code below. Next step is to use the json response and watch the for new releases however the type from the response cannot be unmarshalled?


    package main

    import (
        "context"
        "fmt"
        "github.com/google/go-github/github"
    )

    func main() {
        fmt.Println("start")

        client := github.NewClient(nil)

        opt := &github.ListOptions{Page: 2, PerPage: 10}

        ctx := context.Background()

        rls, resp, err := client.Repositories.ListReleases(ctx, "prometheus-community", "helm-charts", opt)

        if err != nil {
            fmt.Println(err)
        }

        fmt.Println("contents of rls:", rls)
        fmt.Println("contents of resp:", resp)

    }

CodePudding user response:

I'm not sure what you meant exactly by:

type from the response cannot be unmarshalled

Did you receive some kind of an error?

The call to ListReleases returns a []*RepositoryReleases (see code), so you can loop through the response and do whatever you need to with the data.

For example, to list the name of every release:

package main

import (
    "context"
    "fmt"

    "github.com/google/go-github/github"
)

func main() {
    fmt.Println("start")

    client := github.NewClient(nil)

    opt := &github.ListOptions{Page: 2, PerPage: 10}

    ctx := context.Background()

    rls, resp, err := client.Repositories.ListReleases(ctx, "prometheus-community", "helm-charts", opt)

    if err != nil {
        fmt.Println(err)
    }

    for _, release := range rls {
        if release.Name != nil {
            fmt.Println(*release.Name)
        }
    }

}

  • Related