How to get response after http request










3















I'm studying Go and am a real newbie in this field.



I am facing a problem when I try to copy some value.



What I am doing is:




  1. I want to get some response in [response] using httpRequest.



httpClient := &http.Client
response, err := httpClient.Do(req)
if err != nil
panic(err)




  1. After that, I want to save the stored value in response at 'origin.txt'



origin_ ,_:= ioutil.ReadAll(response.Body)
f_, err := os.Create("origin.txt")
f_.Write(origin_);



  1. And I want to get a specific value by using goquery package.



doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil
log.Fatal(err)

doc.Find(".className").Each(func(i int, s *goquery.Selection)
w.WriteString("============" + strconv.Itoa(i) + "============")
s.Find("tr").Each(func(i int, s_ *goquery.Selection)
fmt.Println(s_.Text())
w.WriteString(s_.Text())
)



)



But in this case, I can get a value exactly what I want from 2) but cannot get anything from 3).



At first, I think the problem is, the response object at 3) is affected by 2) action. Because it is a reference object.



So I tried to copy it to another object and then do it again.



origin := *response


but, I got the same result as first.



What should I do?
How can I assign a reference value to another one by its value?



Should I request it twice for each attempt?










share|improve this question




























    3















    I'm studying Go and am a real newbie in this field.



    I am facing a problem when I try to copy some value.



    What I am doing is:




    1. I want to get some response in [response] using httpRequest.



    httpClient := &http.Client
    response, err := httpClient.Do(req)
    if err != nil
    panic(err)




    1. After that, I want to save the stored value in response at 'origin.txt'



    origin_ ,_:= ioutil.ReadAll(response.Body)
    f_, err := os.Create("origin.txt")
    f_.Write(origin_);



    1. And I want to get a specific value by using goquery package.



    doc, err := goquery.NewDocumentFromReader(response.Body)
    if err != nil
    log.Fatal(err)

    doc.Find(".className").Each(func(i int, s *goquery.Selection)
    w.WriteString("============" + strconv.Itoa(i) + "============")
    s.Find("tr").Each(func(i int, s_ *goquery.Selection)
    fmt.Println(s_.Text())
    w.WriteString(s_.Text())
    )



    )



    But in this case, I can get a value exactly what I want from 2) but cannot get anything from 3).



    At first, I think the problem is, the response object at 3) is affected by 2) action. Because it is a reference object.



    So I tried to copy it to another object and then do it again.



    origin := *response


    but, I got the same result as first.



    What should I do?
    How can I assign a reference value to another one by its value?



    Should I request it twice for each attempt?










    share|improve this question


























      3












      3








      3


      1






      I'm studying Go and am a real newbie in this field.



      I am facing a problem when I try to copy some value.



      What I am doing is:




      1. I want to get some response in [response] using httpRequest.



      httpClient := &http.Client
      response, err := httpClient.Do(req)
      if err != nil
      panic(err)




      1. After that, I want to save the stored value in response at 'origin.txt'



      origin_ ,_:= ioutil.ReadAll(response.Body)
      f_, err := os.Create("origin.txt")
      f_.Write(origin_);



      1. And I want to get a specific value by using goquery package.



      doc, err := goquery.NewDocumentFromReader(response.Body)
      if err != nil
      log.Fatal(err)

      doc.Find(".className").Each(func(i int, s *goquery.Selection)
      w.WriteString("============" + strconv.Itoa(i) + "============")
      s.Find("tr").Each(func(i int, s_ *goquery.Selection)
      fmt.Println(s_.Text())
      w.WriteString(s_.Text())
      )



      )



      But in this case, I can get a value exactly what I want from 2) but cannot get anything from 3).



      At first, I think the problem is, the response object at 3) is affected by 2) action. Because it is a reference object.



      So I tried to copy it to another object and then do it again.



      origin := *response


      but, I got the same result as first.



      What should I do?
      How can I assign a reference value to another one by its value?



      Should I request it twice for each attempt?










      share|improve this question
















      I'm studying Go and am a real newbie in this field.



      I am facing a problem when I try to copy some value.



      What I am doing is:




      1. I want to get some response in [response] using httpRequest.



      httpClient := &http.Client
      response, err := httpClient.Do(req)
      if err != nil
      panic(err)




      1. After that, I want to save the stored value in response at 'origin.txt'



      origin_ ,_:= ioutil.ReadAll(response.Body)
      f_, err := os.Create("origin.txt")
      f_.Write(origin_);



      1. And I want to get a specific value by using goquery package.



      doc, err := goquery.NewDocumentFromReader(response.Body)
      if err != nil
      log.Fatal(err)

      doc.Find(".className").Each(func(i int, s *goquery.Selection)
      w.WriteString("============" + strconv.Itoa(i) + "============")
      s.Find("tr").Each(func(i int, s_ *goquery.Selection)
      fmt.Println(s_.Text())
      w.WriteString(s_.Text())
      )



      )



      But in this case, I can get a value exactly what I want from 2) but cannot get anything from 3).



      At first, I think the problem is, the response object at 3) is affected by 2) action. Because it is a reference object.



      So I tried to copy it to another object and then do it again.



      origin := *response


      but, I got the same result as first.



      What should I do?
      How can I assign a reference value to another one by its value?



      Should I request it twice for each attempt?







      go goquery






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 8:34









      poy

      6,55263465




      6,55263465










      asked Nov 16 '18 at 6:43









      JungHoonJungHoon

      1581313




      1581313






















          1 Answer
          1






          active

          oldest

          votes


















          6














          I actually don't see where you use shared resources between 2 and 3.



          However that being said origin := *response won't buy you much. The data (response.Body) is a io.ReadCloser. The ioutil.ReadAll() will consume and store all the data that the stream has. You only get to do this once.



          However you have the data stored in origin. If you need another io.Reader for that data (say for case 3), then you can make that byte slice look like an io.Reader again: bytes.NewReader(origin).






          share|improve this answer

























          • oops sorry. i changed my question... Sorry. The problem was at [response] object!

            – JungHoon
            Nov 16 '18 at 7:02












          • Perfect, well just use bytes.NewReader(origin) as I suggested then.

            – poy
            Nov 16 '18 at 7:11











          • What a nice solution. Using ur solution [func NewReader(b byte) *Reader] this, I can achieve my purpose. Thx!!!

            – JungHoon
            Nov 16 '18 at 8:23











          • I'm glad it worked for you!

            – poy
            Nov 16 '18 at 8:27










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332721%2fhow-to-get-response-after-http-request%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          6














          I actually don't see where you use shared resources between 2 and 3.



          However that being said origin := *response won't buy you much. The data (response.Body) is a io.ReadCloser. The ioutil.ReadAll() will consume and store all the data that the stream has. You only get to do this once.



          However you have the data stored in origin. If you need another io.Reader for that data (say for case 3), then you can make that byte slice look like an io.Reader again: bytes.NewReader(origin).






          share|improve this answer

























          • oops sorry. i changed my question... Sorry. The problem was at [response] object!

            – JungHoon
            Nov 16 '18 at 7:02












          • Perfect, well just use bytes.NewReader(origin) as I suggested then.

            – poy
            Nov 16 '18 at 7:11











          • What a nice solution. Using ur solution [func NewReader(b byte) *Reader] this, I can achieve my purpose. Thx!!!

            – JungHoon
            Nov 16 '18 at 8:23











          • I'm glad it worked for you!

            – poy
            Nov 16 '18 at 8:27















          6














          I actually don't see where you use shared resources between 2 and 3.



          However that being said origin := *response won't buy you much. The data (response.Body) is a io.ReadCloser. The ioutil.ReadAll() will consume and store all the data that the stream has. You only get to do this once.



          However you have the data stored in origin. If you need another io.Reader for that data (say for case 3), then you can make that byte slice look like an io.Reader again: bytes.NewReader(origin).






          share|improve this answer

























          • oops sorry. i changed my question... Sorry. The problem was at [response] object!

            – JungHoon
            Nov 16 '18 at 7:02












          • Perfect, well just use bytes.NewReader(origin) as I suggested then.

            – poy
            Nov 16 '18 at 7:11











          • What a nice solution. Using ur solution [func NewReader(b byte) *Reader] this, I can achieve my purpose. Thx!!!

            – JungHoon
            Nov 16 '18 at 8:23











          • I'm glad it worked for you!

            – poy
            Nov 16 '18 at 8:27













          6












          6








          6







          I actually don't see where you use shared resources between 2 and 3.



          However that being said origin := *response won't buy you much. The data (response.Body) is a io.ReadCloser. The ioutil.ReadAll() will consume and store all the data that the stream has. You only get to do this once.



          However you have the data stored in origin. If you need another io.Reader for that data (say for case 3), then you can make that byte slice look like an io.Reader again: bytes.NewReader(origin).






          share|improve this answer















          I actually don't see where you use shared resources between 2 and 3.



          However that being said origin := *response won't buy you much. The data (response.Body) is a io.ReadCloser. The ioutil.ReadAll() will consume and store all the data that the stream has. You only get to do this once.



          However you have the data stored in origin. If you need another io.Reader for that data (say for case 3), then you can make that byte slice look like an io.Reader again: bytes.NewReader(origin).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 7:10

























          answered Nov 16 '18 at 6:51









          poypoy

          6,55263465




          6,55263465












          • oops sorry. i changed my question... Sorry. The problem was at [response] object!

            – JungHoon
            Nov 16 '18 at 7:02












          • Perfect, well just use bytes.NewReader(origin) as I suggested then.

            – poy
            Nov 16 '18 at 7:11











          • What a nice solution. Using ur solution [func NewReader(b byte) *Reader] this, I can achieve my purpose. Thx!!!

            – JungHoon
            Nov 16 '18 at 8:23











          • I'm glad it worked for you!

            – poy
            Nov 16 '18 at 8:27

















          • oops sorry. i changed my question... Sorry. The problem was at [response] object!

            – JungHoon
            Nov 16 '18 at 7:02












          • Perfect, well just use bytes.NewReader(origin) as I suggested then.

            – poy
            Nov 16 '18 at 7:11











          • What a nice solution. Using ur solution [func NewReader(b byte) *Reader] this, I can achieve my purpose. Thx!!!

            – JungHoon
            Nov 16 '18 at 8:23











          • I'm glad it worked for you!

            – poy
            Nov 16 '18 at 8:27
















          oops sorry. i changed my question... Sorry. The problem was at [response] object!

          – JungHoon
          Nov 16 '18 at 7:02






          oops sorry. i changed my question... Sorry. The problem was at [response] object!

          – JungHoon
          Nov 16 '18 at 7:02














          Perfect, well just use bytes.NewReader(origin) as I suggested then.

          – poy
          Nov 16 '18 at 7:11





          Perfect, well just use bytes.NewReader(origin) as I suggested then.

          – poy
          Nov 16 '18 at 7:11













          What a nice solution. Using ur solution [func NewReader(b byte) *Reader] this, I can achieve my purpose. Thx!!!

          – JungHoon
          Nov 16 '18 at 8:23





          What a nice solution. Using ur solution [func NewReader(b byte) *Reader] this, I can achieve my purpose. Thx!!!

          – JungHoon
          Nov 16 '18 at 8:23













          I'm glad it worked for you!

          – poy
          Nov 16 '18 at 8:27





          I'm glad it worked for you!

          – poy
          Nov 16 '18 at 8:27



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332721%2fhow-to-get-response-after-http-request%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線