英文:
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)
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论