duannuo4620 2016-05-25 23:14
浏览 36
已采纳

Golang,http请求中没有

I'm creating a Go script to get as many http requests as possible from an API.

I'm getting a nil pointer reference at the line of soundcloud.GetUser(i) when I call worker() with multiple goroutines, more than 200 to be exact.

This is the error I'm getting to be exact.

goroutine 28 [running]:
runtime.panic(0x75fca0, 0x98cf13)
/usr/lib/go/src/pkg/runtime/panic.c:279 +0xf5 www.github.com/Juanvulcano/gosoundcloud%2egit.processAndUnmarshalResponses(0x0, 0x7f1d338e9fd0, 0xc209467a10, 0x780a60, 0xc20809c300, 0x0, 0x0)
      /home/maker/go/src/www.github.com/Juanvulcano/gosoundcloud.git/gosoundcloud.go:31 +0x413
www.github.com/Juanvulcano/gosoundcloud%2egit.(*SoundcloudApi).GetUser(0xc208040520, 0x2, 0x7f1d41e81f40, 0x0, 0x0)
    /home/maker/go/src/www.github.com/Juanvulcano/gosoundcloud.git/soundcloud.go:295 +0x1b1
main.worker()
    /home/maker/go/src/GoBot/GoBot.go:28 +0xdd
created by main.main
    /home/maker/go/src/GoBot/GoBot.go:71 +0x55d

Here I'm attaching the important parts of my code.

func worker() {
   defer wg.Done()
   for i := range input {
       member,  err := soundcloud.GetUser(uint64(i))
       if err != nil{
           fmt.Println(err)
       }
       if member != nil {
           output <- fmt.Sprint(member.Username)
       }
    }
}    


func main() {    
    var err error
    if err = soundcloud.PasswordCredentialsToken("email", "password"); err != nil {
        fmt.Println(err)
        os.Exit(1)
    }  
    go func() {    
        for i := 1; i < 1000; i++ {
                input <- i
       }
       close(input)
       wg.Wait()
       close(output)
    }()

    for i := 0; i < 200; i++ {
        wg.Add(1)
        go worker()
    }    
    printOutput()
}
  • 写回答

1条回答 默认 最新

  • doupai6875 2016-05-26 00:26
    关注

    in the function:

    func processAndUnmarshalResponses(resp *http.Response, err error, holder interface{}) error {
        defer resp.Body.Close()
        if err != nil {
            return err
        }
    

    You are defering resp.Body.Close() before you check the error. If there is an error, resp will be nil, and dereferencing it will panic. Always put the error check first.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器