需要使用Go和go-github来过滤一些数据,对于响应的下一步操作感到困惑。

huangapple go评论68阅读模式
英文:

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

问题

我正在使用go-gihub库和Go语言编写代码,成功地列出了下面代码中示例仓库的一些发布版本。下一步是使用JSON响应并监视新的发布版本,但是无法对响应的类型进行解组。

package main

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

func main() {
	fmt.Println("开始")

	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("rls的内容:", rls)
	fmt.Println("resp的内容:", resp)

}
英文:

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)

    }

答案1

得分: 0

我不确定你的意思是什么:

> type from the response cannot be unmarshalled

你收到了某种错误吗?

调用ListReleases返回一个[]*RepositoryReleases查看代码),所以你可以遍历响应并根据需要处理数据。

例如,要列出每个发布的名称:

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)
		}
	}

}
英文:

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)
		}
	}

}

huangapple
  • 本文由 发表于 2021年12月9日 00:53:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/70278793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定