1
votes

I am attempting to run a conditional to basically see if the object is empty but I keep getting (similar variations) of this error:

invalid operation: release.Name == "" (mismatched types *string and string)

Here is the code that is dying:

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

func TestLatestTag(user, project string) {

    var client *github.Client
    client = github.NewClient(nil)

    releases, _, err := client.Repositories.ListTags(user, project, nil)
    var release github.RepositoryTag

    if err != nil {
        fmt.Println("Error")
    } else {
        if release.Name == "" {
            fmt.Println("None")
        } else {
            fmt.Println(releases[0])
        }
    }
}

If I change the if statement to *release.Name == "" as the error suggests I get a different error, which I don't really understand:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x26fd]

goroutine 1 [running]:

I'm sure there is any easy way to do this but I am not very familiar with handling objects/structs

3

3 Answers

4
votes

From the error message it looks like you are trying to compare a string pointer (*string) to an actual string.

  • release.Name is a *string (a pointer to a string value)
  • "" is a string (is a string value)

They are two different types. So you can't compare them.

What you probably want to do instead is release.Name == nil

When a pointer that references to nothing (equals to nil) is tried to be dereferenced you get that second error. So in your case *release.Name panics because infact release.Name is nil

3
votes
var release github.RepositoryTag

You never assign any value to that var. That's why *release.Name gives you a "runtime error": release.Name is a nil pointer

1
votes

As per your code you have declared var release github.RepositoryTag, but you have not initialized it.

In structure RepositoryTag, Name is declared as *string which is a pointer and in case of release.Name == "", string comparison is attempted which is incorrect hence "mismatched types *string and string" error.

In case of *release.Name == "", since release is not yet initialized, it is complaining "invalid memory address or nil pointer dereference"

You need to do two things, 1st initialize, release and second, check release.Name = nil.