Go HTTP批处理从多个API获取并保存到结构

Go HTTP批处理从多个API获取并保存到结构,go,Go,我使用以下函数获取URL并将数据返回到接口(例如struct/int/where): 然后我有几个函数如下所示: func GetCustomerByID(APIKey, cusID string) { cus := new(Customer) getURLToTarget(fmt.Sprintf("http://someurl.com/%s/customerbyid/:%s", APIKey, cusID), &cus) } type Customer struct {

我使用以下函数获取URL并将数据返回到接口(例如struct/int/where):

然后我有几个函数如下所示:

func GetCustomerByID(APIKey, cusID string) {
  cus := new(Customer)
  getURLToTarget(fmt.Sprintf("http://someurl.com/%s/customerbyid/:%s", APIKey, cusID), &cus)
}
type Customer struct {
  Name string
  Email string
  Address string
}
在本例中,它将json响应保存到如下结构中:

func GetCustomerByID(APIKey, cusID string) {
  cus := new(Customer)
  getURLToTarget(fmt.Sprintf("http://someurl.com/%s/customerbyid/:%s", APIKey, cusID), &cus)
}
type Customer struct {
  Name string
  Email string
  Address string
}
现在我的问题是,当我运行时,如何使所有这些http请求同时执行:

func main() {
  apikey := "some api key"
  GetCustomerByID(apikey, "43279843")
  GetCustomerDiscounts(apikey, "43279843")
  GetProductByID(apikey, "32124")
}

我很确定我需要使用频道,但我不知道如何。。任何帮助都将不胜感激

实现这一点的方法有很多,它基于您需要发生的事情

最基本的方法是使用goroutines和wg.WaitGroup并行/并发地进行http调用,并在退出程序之前等待所有调用完成。例如:

func main() {
  apikey := "some api key"

  var wg sync.WaitGroup
  wg.Add(3)

  go func() {
    GetCustomerByID(apikey, "43279843")
    wg.Done()
  }()

  go func() {
    GetCustomerDiscounts(apikey, "43279843")
    wg.Done()
  }()

  go func() {
    GetProductByID(apikey, "32124")
    wg.Done()
  }()

  wg.Wait()
}
func GetCustomerByID(APIKey, cusID string) Customer {
  cus := new(Customer)
  getURLToTarget(fmt.Sprintf("http://someurl.com/%s/customerbyid/:%s", APIKey, cusID), &cus)
  return cus
}

func main() {
  apikey := "some api key"

  c := make(chan Customer, 3)

  go func() {
    cus := GetCustomerByID(apikey, "43279843")
    c <- cus
  }()

  go func() {
    cus := GetCustomerDiscounts(apikey, "43279843")
    c <- cus
  }()

  go func() {
    cus := GetProductByID(apikey, "32124")
    c <- cus
  }()

  // Print the result
  var i int
  for cus := range c {
    fmt.Printf("%#v\n", cus)
    i++

    if i == 3 {
      break
    }
  }
}
如果要检查每个http调用的结果,另一种方法是使用go通道。例如:

func main() {
  apikey := "some api key"

  var wg sync.WaitGroup
  wg.Add(3)

  go func() {
    GetCustomerByID(apikey, "43279843")
    wg.Done()
  }()

  go func() {
    GetCustomerDiscounts(apikey, "43279843")
    wg.Done()
  }()

  go func() {
    GetProductByID(apikey, "32124")
    wg.Done()
  }()

  wg.Wait()
}
func GetCustomerByID(APIKey, cusID string) Customer {
  cus := new(Customer)
  getURLToTarget(fmt.Sprintf("http://someurl.com/%s/customerbyid/:%s", APIKey, cusID), &cus)
  return cus
}

func main() {
  apikey := "some api key"

  c := make(chan Customer, 3)

  go func() {
    cus := GetCustomerByID(apikey, "43279843")
    c <- cus
  }()

  go func() {
    cus := GetCustomerDiscounts(apikey, "43279843")
    c <- cus
  }()

  go func() {
    cus := GetProductByID(apikey, "32124")
    c <- cus
  }()

  // Print the result
  var i int
  for cus := range c {
    fmt.Printf("%#v\n", cus)
    i++

    if i == 3 {
      break
    }
  }
}
func getcustomerbyd(APIKey,cusID字符串)客户{
客户:=新客户
getURLToTarget(fmt.Sprintf(“http://someurl.com/%s/customerbyid/:%s,APIKey,cusID),&cus)
返回cus
}
func main(){
apikey:=“某些api密钥”
c:=制造(陈客户,3)
go func(){
cus:=getcustomerbyd(apikey,“43279843”)

c哦,Go有一个很好的关键字,用来同时运行东西,它叫
Go
。我建议你在Go之旅中学习,它比任何答案都能更好地解释这一点。假设你参考Tour.golang.org/concurrency,那么我认为这是任何人(包括我自己)首先要去的地方。但在我的情况下,这是(以及我读过的在线文章还不够)假设你只是想在函数调用前添加'go',那么我99%确定它不会导致批量拉取,因此它仍然发出X个请求(在本例中是3个),而不是使用1个requestUuum,那么:1.获取2.存储
req.Body
3.运行
json.NewDecoder(请求正文)。解码(目标)
对于要存储在数据库上的每个结构body@fisker在你的问题中,你问了
当我运行时,如何使所有这些http请求同时执行:…
,@Volker的注释是正确的,在你的函数调用前面加上
go
关键字,这3个函数将同时执行。不过,从你的注释来看,您似乎只想发出1个HTTP请求而不是3个,如果您发出这些请求的api没有为批处理资源提供端点,这是不可能的。也许我很愚蠢,但我认为这种方式仍然会发送3个请求而不是1个请求?我认为有一个与批处理HTTP请求相关的概念,这样可以代替发送Gx单独的请求(这与您的答案并行),然后您只发送一个请求来完成所有任务?抱歉,也许我不了解您的要求,这里您只想对3个不同的http请求进行一次http调用?是的,但是@mkopriva说这是不可能的(至少不是我认为的方式)因此,考虑到目前的情况,我想你的答案可能达到最佳结果:)