Golang:同时进行的函数调用用于HTTP POST请求。

huangapple go评论121阅读模式
英文:

Golang: Simultaneous function Calls for http post request

问题

我需要同时调用多个URL。我的函数在同一时间(以毫秒为单位)被调用,但是当我在代码中添加一个HTTP POST请求时,它们会一个接一个地被调用。以下是代码:

  1. Check(url1)
  2. Check(url2)
  3. func Check(xurl string) {
  4. nowstartx := time.Now()
  5. startnanos := nowstartx.UnixNano()
  6. nowstart := startnanos / 1000000
  7. fmt.Println(nowstart)
  8. json = {"name" : "test"}
  9. req, err := http.NewRequest("POST", xurl, bytes.NewBuffer(json))
  10. req.Header.Set("X-Custom-Header", "myvalue")
  11. req.Header.Set("Content-Type", "application/json")
  12. client := &http.Client{}
  13. resp, err := client.Do(req)
  14. if err != nil {
  15. panic(err)
  16. } else {
  17. defer resp.Body.Close()
  18. body, _ = ioutil.ReadAll(resp.Body)
  19. }
  20. }

感谢帮助,我需要在运行程序时获得相同的时间(以毫秒为单位)。

英文:

I need to call multiple URL at the same time. My functions get called at the same time (in milli seconds) but the moment I add a Http post request to the code it gets called one after the other. Below is the code:

  1. Check(url1)
  2. Check(url2)
  3. func Check(xurl string) {
  4. nowstartx := time.Now()
  5. startnanos := nowstartx.UnixNano()
  6. nowstart := startnanos / 1000000
  7. fmt.Println(nowstart)
  8. json = {"name" : "test"}
  9. req, err := http.NewRequest("POST", xurl, bytes.NewBuffer(json))
  10. req.Header.Set("X-Custom-Header", "myvalue")
  11. req.Header.Set("Content-Type", "application/json")
  12. client := &http.Client{}
  13. resp, err := client.Do(req)
  14. if err != nil {
  15. panic(err)
  16. } else {
  17. defer resp.Body.Close()
  18. body, _ = ioutil.ReadAll(resp.Body)
  19. }
  20. }

Appreciate help, I need to get the same time (in milliseconds) when I run the program.

答案1

得分: 3

这是通过使用Goroutines实现的。

  1. go Check(url1)
  2. go Check(url2)
  3. func Check(xurl string) {
  4. nowstartx := time.Now()
  5. startnanos := nowstartx.UnixNano()
  6. nowstart := startnanos / 1000000
  7. fmt.Println(nowstart)
  8. json = {"name" : "test"}
  9. req, err := http.NewRequest("POST", xurl, bytes.NewBuffer(json))
  10. req.Header.Set("X-Custom-Header", "myvalue")
  11. req.Header.Set("Content-Type", "application/json")
  12. client := &http.Client{}
  13. resp, err := client.Do(req)
  14. if err != nil {
  15. panic(err)
  16. } else {
  17. defer resp.Body.Close()
  18. body, _ = ioutil.ReadAll(resp.Body)
  19. }
  20. }
英文:

This is achieved by using Goroutines

  1. go Check(url1)
  2. go Check(url2)
  3. func Check(xurl string) {
  4. nowstartx := time.Now()
  5. startnanos := nowstartx.UnixNano()
  6. nowstart := startnanos / 1000000
  7. fmt.Println(nowstart)
  8. json = {"name" : "test"}
  9. req, err := http.NewRequest("POST", xurl, bytes.NewBuffer(json))
  10. req.Header.Set("X-Custom-Header", "myvalue")
  11. req.Header.Set("Content-Type", "application/json")
  12. client := &http.Client{}
  13. resp, err := client.Do(req)
  14. if err != nil {
  15. panic(err)
  16. } else {
  17. defer resp.Body.Close()
  18. body, _ = ioutil.ReadAll(resp.Body)
  19. }
  20. }

huangapple
  • 本文由 发表于 2016年7月20日 21:27:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/38482664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定