建设工程专业承包交易中心网站,网页特效代码,网页设计制作网站教程,网站建设企业网站建设上周#xff0c;我发布了一篇关于如何直观解释Golang中通道#xff08;Channel#xff09;的文章。如果你对通道仍然感到困惑#xff0c;请先查看那篇文章。 Go并发可视化解释 — Channel 作为一个快速复习#xff1a;Partier、Candier和Stringer经营着一家咖啡店。Partie… 上周我发布了一篇关于如何直观解释Golang中通道Channel的文章。如果你对通道仍然感到困惑请先查看那篇文章。 Go并发可视化解释 — Channel 作为一个快速复习Partier、Candier和Stringer经营着一家咖啡店。Partier负责接受顾客的订单然后将这些订单传递给厨房Candier和Stringer制作咖啡。  Gophers CafeGopher咖啡馆 在本文中我将直观解释select语句这是在Go应用程序中处理并发的另一个强大工具。Gophers和他们的虚构咖啡馆仍然是我的伙伴但这次让我们聚焦在Partier和点单部分。 情景 Gopher的Cafe意识到越来越多的顾客希望通过外卖应用程序在线订购咖啡。因此除了店内点餐外他们还选择了一个外卖应用程序。Partier会监视来自两个通道的订单并通过另一个名为queue的通道将这些订单转发给Candier和Stringer。 select {
case order : -appOrders:queue - order
case order : -inShopOrders:queue - order
} 当这两个通道中的任何一个有订单时Partier会获取订单并将其转发到queue通道。   如果这两个通道都有订单将会选择其中一个。在实际的咖啡店中来自inShopOrders的订单可能会被优先处理。但是在Go应用程序中我们无法保证哪个订单会被选择。还要注意select语句的执行只会选择一个订单Partier不会一次选择两个订单。但是在许多应用程序中select语句通常嵌套在for循环中以便在前一个迭代中剩下的订单有机会在下一个迭代中被选择。 for {select {case order : -appOrders:queue - ordercase order : -inShopOrders:queue - order}
} 但是如果这两个通道都有订单它们将再次进行公平竞争。  默认情况Default 在非高峰时段订单不多Partier花费大量时间在等待上。他认为他可以通过做其他事情来更有效地利用时间例如清理桌子。这可以通过default来实现。 for {select {case order : -appOrders:log.Println(There is an order coming from appOrders channel)queue - ordercase order : -inShopOrders:log.Println(There is an order coming from inShopOrders channel)queue - orderdefault:log.Println(There is no order on both channels, I will do cleaning instead)doCleaning()}
} time.After() time.After(duration)通常与select一起使用以防止永久等待。与default不同time.After(duration)会创建一个普通的-chan Time等待duration时间的流逝然后将当前时间发送到返回的通道上。这个通道在select语句中与其他通道平等对待。正如你所看到的select语句中的通道可以是不同类型的。 shouldClose : false
closeHourCh : time.After(8 * time.Hour)for !shouldClose {select {case order : -appOrders:log.Println(There is an order coming from appOrders channel)queue - ordercase order : -inShopOrders:log.Println(There is an order coming from inShopOrders channel)queue - ordercase now : -closeHourCh:log.Printf(It is %v now, the shop is closing\n, now)shouldClose  truedefault:log.Println(There is no order on both channels, I will go cleaning instead)doCleaning()}
}log.Println(Shop is closed, Im going home now. Bye!) 当处理远程API调用时这种技术非常常见因为我们无法保证远程服务器何时返回或是否返回。借助于context通常不需要这样做。 responseChannel : make(chan interface{})
timer : time.NewTimer(timeout)select {
case resp : -