我试图运行一个条件来基本上查看对象是否为空,但我不断收到此错误(类似的变体):
invalid operation: release.Name == "" (mismatched types *string and string)
Run Code Online (Sandbox Code Playgroud)
这是即将消亡的代码:
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])
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我将 if 语句更改为*release.Name == ""错误所暗示的那样,我会得到一个不同的错误,我不太明白:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x26fd]
goroutine 1 [running]:
Run Code Online (Sandbox Code Playgroud)
我确信有任何简单的方法可以做到这一点,但我不太熟悉处理对象/结构
从错误消息来看,您似乎正在尝试将string pointer( *string) 与实际的string.
release.Name是一个*string(指向值的指针string)""是一个string(是一个string值)它们是两种不同的类型。所以你无法比较它们。
你可能想做的是release.Name == nil
当尝试取消引用没有引用任何内容(等于)的指针时,您会收到第二个错误。所以在你的情况下会恐慌,因为事实上是nil*release.Namerelease.Namenil
| 归档时间: |
|
| 查看次数: |
15785 次 |
| 最近记录: |