类型不匹配 *string 和 string

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

mismatched types *string and string

问题

我正在尝试运行一个条件语句,基本上是为了判断对象是否为空,但我一直得到类似的错误(错误的变体):

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

以下是出错的代码:

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语句更改为*release.Name == "",如错误所建议的那样,我会得到一个不同的错误,我不太理解:

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

goroutine 1 [running]:

我确定有一种简单的方法来做到这一点,但我对处理对象/结构不太熟悉。

英文:

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

答案1

得分: 7

从错误信息来看,你似乎在尝试将一个string指针(*string)与一个实际的string进行比较。

  • release.Name是一个*string(指向string值的指针)
  • ""是一个string(是一个string值)

它们是两种不同的类型,所以你不能将它们进行比较。

你可能想要做的是release.Name == nil

当一个指向空值(等于nil)的指针被解引用时,你会得到第二个错误。所以在你的情况下,*release.Name会引发恐慌,因为实际上release.Namenil

英文:

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

答案2

得分: 3

你从未给那个变量赋任何值。这就是为什么 *release.Name 给出了一个"运行时错误":release.Name 是一个空指针。

英文:
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

答案3

得分: 3

根据你的代码,你声明了一个变量 release github.RepositoryTag,但是你没有对它进行初始化。

RepositoryTag 结构体中,Name 被声明为 *string 类型的指针,而在 release.Name == "" 的比较中,尝试进行字符串比较是不正确的,因此会出现 "mismatched types *string and string" 的错误。

*release.Name == "" 的情况下,由于 release 还没有被初始化,会出现 "invalid memory address or nil pointer dereference" 的错误。

你需要做两件事:首先初始化 release,其次检查 release.Name 是否为 nil

英文:

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.

huangapple
  • 本文由 发表于 2015年11月22日 16:12:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/33852854.html
匿名

发表评论

匿名网友

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

确定