From c2e125c82d10372bafc8284521b4280f55217fbf Mon Sep 17 00:00:00 2001 From: yanzuoguang Date: Tue, 7 Oct 2025 21:11:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(main):=20=E6=B7=BB=E5=8A=A0=E5=B9=B6?= =?UTF-8?q?=E5=8F=91=E8=AE=A1=E7=AE=97=E5=92=8C=E8=AE=A2=E5=8D=95=E5=88=86?= =?UTF-8?q?=E5=8F=91=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 operations1.go 文件实现带通道的并发价格计算 - 新增 orderdispatch.go 文件实现订单分发和客户通知- 修改 main.go 调用新的并发计算和订单分发函数 - 更新 product.go 中 "Unsteady Chair"价格从 29.95 到 75 - 修正 product.go 中产品组初始化逻辑,确保正确添加新产品组- 在 CalcStoreTotal 函数中增加执行开始提示信息- 在 TotalPrice 方法中增加产品名称打印信息 - main 函数中调整执行顺序并增加换行符以改善输出格式 --- 14-concurrency/concurrency/main.go | 9 +++- 14-concurrency/concurrency/operations.go | 2 + 14-concurrency/concurrency/operations1.go | 36 +++++++++++++ 14-concurrency/concurrency/orderdispatch.go | 58 +++++++++++++++++++++ 14-concurrency/concurrency/product.go | 4 +- 5 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 14-concurrency/concurrency/operations1.go create mode 100644 14-concurrency/concurrency/orderdispatch.go diff --git a/14-concurrency/concurrency/main.go b/14-concurrency/concurrency/main.go index 4e64aab..8ba081b 100644 --- a/14-concurrency/concurrency/main.go +++ b/14-concurrency/concurrency/main.go @@ -1,9 +1,14 @@ package main -import "fmt" +import ( + "fmt" +) func main() { fmt.Println("main function started") CalcStoreTotal(Products) - fmt.Println("main function complete") + CalcStoreTotal1(Products) + main1() + main2() + fmt.Println("\nmain function complete") } diff --git a/14-concurrency/concurrency/operations.go b/14-concurrency/concurrency/operations.go index 5a902c2..732a6b5 100644 --- a/14-concurrency/concurrency/operations.go +++ b/14-concurrency/concurrency/operations.go @@ -3,6 +3,7 @@ package main import "fmt" func CalcStoreTotal(data ProductData) { + fmt.Println("\nCalcStoreTotal function started:") var storeTotal float64 for category, group := range data { storeTotal += group.TotalPrice(category) @@ -12,6 +13,7 @@ func CalcStoreTotal(data ProductData) { func (group ProductGroup) TotalPrice(category string) (total float64) { for _, p := range group { + fmt.Println(category, "product:", p.Name) total += p.Price } fmt.Println(category, "subtotal: ", ToCurrency(total)) diff --git a/14-concurrency/concurrency/operations1.go b/14-concurrency/concurrency/operations1.go new file mode 100644 index 0000000..7abb8ae --- /dev/null +++ b/14-concurrency/concurrency/operations1.go @@ -0,0 +1,36 @@ +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 +} diff --git a/14-concurrency/concurrency/orderdispatch.go b/14-concurrency/concurrency/orderdispatch.go new file mode 100644 index 0000000..3d3b287 --- /dev/null +++ b/14-concurrency/concurrency/orderdispatch.go @@ -0,0 +1,58 @@ +package main + +import ( + "fmt" + "math/rand" + "time" +) + +type DispatchNotification struct { + Customer string + *Product + Quantity int +} + +var Customers = []string{"Alice", "Bob", "Charlie", "David", "Edith", "Fred"} + +func DispatchOrders(channel chan<- DispatchNotification) { + rand.Seed(time.Now().UTC().UnixNano()) + orderCount := rand.Intn(3) + 2 + fmt.Println("Order count:", orderCount) + for i := 0; i < orderCount; i++ { + channel <- DispatchNotification{ + Customer: Customers[rand.Intn(len(Customers))], + Quantity: rand.Intn(5) + 1, + Product: ProductList[rand.Intn(len(ProductList))], + } + //if i == 1 { + // notification := <-channel + // fmt.Println("Read:", notification.Customer) + //} + } + + close(channel) +} + +func main1() { + fmt.Println("\nmain1") + dispatchChannel := make(chan DispatchNotification, 100) + go DispatchOrders(dispatchChannel) + for { + if details, open := <-dispatchChannel; open { + fmt.Println("Dispatching order to", details.Customer, ":", details.Product.Name, "x", details.Quantity) + } else { + fmt.Println("Channel has been closed") + break + } + } +} + +func main2() { + fmt.Println("\nmain2") + dispatchChannel := make(chan DispatchNotification, 100) + go DispatchOrders(dispatchChannel) + for details := range dispatchChannel { + fmt.Println("Dispatching order to", details.Customer, ":", details.Product.Name, "x", details.Quantity) + } + fmt.Println("Channel has been closed") +} diff --git a/14-concurrency/concurrency/product.go b/14-concurrency/concurrency/product.go index 29cf843..1136b42 100644 --- a/14-concurrency/concurrency/product.go +++ b/14-concurrency/concurrency/product.go @@ -16,7 +16,7 @@ var ProductList = []*Product{ {"Corner Flags", "Soccer", 34.95}, {"Stadium", "Soccer", 79500}, {"Thinking Cap", "Chess", 16}, - {"Unsteady Chair", "Chess", 29.95}, + {"Unsteady Chair", "Chess", 75}, {"Bling-Bling King", "Chess", 1200}, } @@ -33,7 +33,7 @@ func init() { if _, ok := Products[product.category]; ok { Products[product.category] = append(Products[product.category], product) } else { - Products[product.category] = ProductGroup{} + Products[product.category] = ProductGroup{product} } } }