Go-github not retrieving tag names correctly

65 Views Asked by At

I have the following simple golang code which retrieves tags from terraform repository:

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

Which returns a strange hexadecimal value: 0x1400035c4a0 And I would want to return: v1.4.0-alpha20221207

Following the official docs, the function ListTags should return the name encoded into a struct: https://pkg.go.dev/github.com/google/go-github/github#RepositoriesService.ListTags

Many thanks

I did try to execute a simple GET request https://api.github.com/repos/hashicorp/terraform/tags and I can see that the github api returns the tags correctly

1

There are 1 best solutions below

0
On

IDK why, but I realize the latestTag.Name is a pointer and what you're printing is the address of the memory: 0x1400035c4a0.

You just need to dereference it:

fmt.Println(*latestTag.Name)

Bonus, check error with if condition that is returned by the function call to avoid having to go something like this:

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