恐慌:运行时错误:无效的内存地址或无指针取消引用

时间:2016-05-15 01:09:47

标签: pointers go soundcloud

我目前正在使用Go的Soundcloud包装器,我想打印用户的关注者,但这是我第一次遇到指针问题。

构建错误后

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

代码

package main

import (
    "fmt"
    "github.com/njasm/gosoundcloud"
)


func main() {
    //  callback url is optional - nil in example
    s, _ := gosoundcloud.NewSoundcloudApi("Client_Id", "Client_Secret", nil)
    var userID uint64 = 1
    member, err := s.GetUser(userID)
    if err != nil {
               panic(err)
     }
    fmt.Println(member.Followers)
}

建成后

goroutine 1 [running]:
panic(0x3508c0, 0xc82000a0b0)
    /usr/local/go/src/runtime/panic.go:481 +0x3e6
net/http.(*Client).doFollowingRedirects(0x0, 0xc8200d0000, 0x4611c8, 0x0, 0x0, 0x0)
    /usr/local/go/src/net/http/client.go:429 +0x66
net/http.(*Client).Do(0x0, 0xc8200d0000, 0x8, 0x0, 0x0)
    /usr/local/go/src/net/http/client.go:188 +0xff
github.com/njasm/gosoundcloud.(*SoundcloudApi).do(0xc8200c8720, 0xc8200d0000, 0xc820012f00, 0x0, 0x0)
    /Users/ManuelDao/Documents/GoBot/src/github.com/njasm/gosoundcloud/soundcloud.go:217 +0x4b
github.com/njasm/gosoundcloud.(*SoundcloudApi).Get(0xc8200c8720, 0xc820012f00, 0x22, 0x0, 0x1, 0x0, 0x0)
    /Users/ManuelDao/Documents/GoBot/src/github.com/njasm/gosoundcloud/soundcloud.go:92 +0xe1
github.com/njasm/gosoundcloud.(*SoundcloudApi).GetUser(0xc8200c8720, 0x1, 0x417a80, 0x0, 0x0)
        /Users/ManuelDao/Documents/GoBot/src/github.com/njasm/gosoundcloud/soundcloud.go:276 +0xc6
main.main()
        /Users/ManuelDao/Documents/GoBot/src/GoBot/GoBot.go:24 +0x70
exit status 2

1 个答案:

答案 0 :(得分:1)

我之前从未使用过此lib,但您似乎忘记了自己验证。

s, _ := gosoundcloud.NewSoundcloudApi("Client_Id", "Client_Secret", nil)
if err = s.PasswordCredentialsToken("your_email@something.com", "your_password"); err != nil {
    // handle err
}

这样的事可能吗?