- 新增 operations1.go 文件实现带通道的并发价格计算 - 新增 orderdispatch.go 文件实现订单分发和客户通知- 修改 main.go 调用新的并发计算和订单分发函数 - 更新 product.go 中 "Unsteady Chair"价格从 29.95 到 75 - 修正 product.go 中产品组初始化逻辑,确保正确添加新产品组- 在 CalcStoreTotal 函数中增加执行开始提示信息- 在 TotalPrice 方法中增加产品名称打印信息 - main 函数中调整执行顺序并增加换行符以改善输出格式
37 lines
971 B
Go
37 lines
971 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
func CalcStoreTotal1(data ProductData) {
|
|
fmt.Println("\nCalcStoreTotal1 function started:")
|
|
var storeTotal float64
|
|
var channel chan float64 = make(chan float64, 2)
|
|
for category, group := range data {
|
|
go group.TotalPrice1(category, channel)
|
|
}
|
|
for i := 0; i < len(data); i++ {
|
|
fmt.Println("-- channel read pending", len(channel), "items in buffer,size", cap(channel))
|
|
value := <-channel
|
|
fmt.Println("-- channel read complete", value)
|
|
storeTotal += value
|
|
time.Sleep(time.Second)
|
|
}
|
|
fmt.Println("Total: ", ToCurrency(storeTotal))
|
|
}
|
|
|
|
func (group ProductGroup) TotalPrice1(category string, resultChannel chan float64) {
|
|
var total float64
|
|
for _, p := range group {
|
|
// fmt.Println(category, "product:", p.Name)
|
|
total += p.Price
|
|
time.Sleep(time.Millisecond * 100)
|
|
}
|
|
fmt.Println(category, "channel sending: ", ToCurrency(total))
|
|
resultChannel <- total
|
|
fmt.Println(category, "channel send complete")
|
|
return
|
|
}
|