0

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()
}
3
  • Hi there Karrot, it's a custom struct I'm attaching the references here. github.com/njasm/gosoundcloud/blob/master/soundcloud.go It is NewSoundcloudApi Commented May 25, 2016 at 23:29
  • @KarrotKake I knew it must be something like that! Thanks a lot Commented May 25, 2016 at 23:41
  • @KarrotKake The race detector didn't told me anything. I'm running the binary with -race and also checked the package. Why do you say there's a race? Commented May 25, 2016 at 23:58

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.