samedi 26 juin 2021

Golang request.Body.Close() returns an empty Document

I have 2 methods in 2 different packages, where func B() takes a url reads the web page and returns *html.Tokenizer. But the problem is, it is working fine Only when I comment the defer r.Body.Close(), If I enable it this doc returned from func B is empty.

And it also works if both the functions are merged in single function. but I need them in 2 different package.

Any suggestion or Idea that What am I missing here ? shoudn't the res.Body be closed ?

func  (s ParserService) A(u string) (*domain.Result, error) {
    doc, err := s.B("https://www.google.com/")
    if err != nil {
        fmt.Println(err.Error())
    }
    for tokenType := doc.Next(); tokenType != html.ErrorToken; {
        token := doc.Token()
        fmt.Println(token)
        tokenType = doc.Next()
    }
}

func (c Downloader) B(url string) (*html.Tokenizer, error) {
    r, err := c.httpClient.Get(url)
    if err != nil {
        return nil, err
    }
//    defer r.Body.Close()
    doc := html.NewTokenizer(r.Body)
    return doc, nil
}



Aucun commentaire:

Enregistrer un commentaire