Go-github没有正确检索标签名称



我有以下简单的golang代码,用于从地形存储库检索标记:

import (
"github.com/google/go-github/v48/github"
"context"
)
func main() { 
client := github.NewClient(nil)
tags, _, _ := client.Repositories.ListTags(context.Background(), "hashicorp", "terraform", nil)
if len(tags) > 0 {
latestTag := tags[0]
fmt.Println(latestTag.Name)
} else {
fmt.Printf("No tags yet")
}
}

返回一个奇怪的十六进制值:0x1400035c4a0我还想说:v1.4.0-alpha20221207

按照官方文档,函数ListTags应该返回被编码成struct的名称:https://pkg.go.dev/github.com/google/go-github/github RepositoriesService.ListTags

多谢

我确实尝试执行一个简单的GET请求https://api.github.com/repos/hashicorp/terraform/tags,我可以看到github api正确返回标签

IDK为什么,但我意识到latestTag.Name是一个指针,你正在打印的是内存的地址:0x1400035c4a0

你只需要解引用它:

fmt.Println(*latestTag.Name)
额外的,用函数调用返回的if条件检查error,以避免出现这样的情况:
tags, response, err := client.Repositories.ListTags(context.Background(), "hashicorp", "terraform", nil)
fmt.Println(response)
if err != nil {
fmt.Println(err)
} else {
if len(tags) > 0 {
latestTag := tags[0]
fmt.Println(*latestTag.Name)
} else {
fmt.Printf("No tags yet")
}
}

最新更新