pass total over channel
Vladimir Bauer
8 years ago
| 28 | 28 | ), |
| 29 | 29 | ) |
| 30 | 30 | |
| 31 | totalUpd1 := make(chan struct{}) | |
| 32 | totalUpd2 := make(chan struct{}) | |
| 31 | totalUpd := make(chan int64) | |
| 33 | 32 | go func() { |
| 34 | <-totalUpd1 | |
| 35 | // intermediate not final total update | |
| 36 | bar.SetTotal(200, false) | |
| 37 | <-totalUpd2 | |
| 38 | // final total update | |
| 39 | bar.SetTotal(300, true) | |
| 33 | for { | |
| 34 | total, ok := <-totalUpd | |
| 35 | bar.SetTotal(total, !ok) | |
| 36 | if !ok { | |
| 37 | break | |
| 38 | } | |
| 39 | } | |
| 40 | 40 | }() |
| 41 | 41 | |
| 42 | 42 | max := 100 * time.Millisecond |
| 43 | 43 | for i := 0; i < 300; i++ { |
| 44 | 44 | if i == 140 { |
| 45 | close(totalUpd1) | |
| 45 | totalUpd <- 190 | |
| 46 | 46 | } |
| 47 | 47 | if i == 250 { |
| 48 | close(totalUpd2) | |
| 48 | totalUpd <- 300 | |
| 49 | // final upd, so closing channel | |
| 50 | close(totalUpd) | |
| 49 | 51 | } |
| 50 | 52 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) |
| 51 | 53 | bar.Increment() |