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} } } }