Last active
May 21, 2018 01:21
-
-
Save trstringer/6251ddf62faff41cfa7358e46eea8d85 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "sync" | |
| "time" | |
| ) | |
| func main() { | |
| fmt.Println("Starting program...") | |
| maxItemCount := 10 | |
| wg := sync.WaitGroup{} | |
| processed := make(chan int) | |
| final := []int{} | |
| for i := 0; i < maxItemCount; i++ { | |
| wg.Add(1) | |
| go doSomething(&wg, i, processed) | |
| } | |
| go func() { | |
| for item := range processed { | |
| // Exaggerate a long processing operation when consuming channel items | |
| time.Sleep(5 * time.Second) | |
| fmt.Printf("Processed %d\n", item) | |
| final = append(final, item) | |
| } | |
| }() | |
| wg.Wait() | |
| close(processed) | |
| fmt.Printf("Final: %v\n", final) | |
| fmt.Println("-------------------") | |
| fmt.Printf("Initial item count: %d\n", maxItemCount) | |
| fmt.Printf("Final item count: %d\n", len(final)) | |
| } | |
| func doSomething(wg *sync.WaitGroup, input int, output chan<- int) { | |
| defer wg.Done() | |
| fmt.Printf("[%d] Starting DoSomething\n", input) | |
| fmt.Printf("[%d] Starting long processing\n", input) | |
| time.Sleep(2 * time.Second) | |
| fmt.Printf("[%d] Finished long processing\n", input) | |
| output <- input * 100 | |
| } | |
| /* | |
| Starting program... | |
| [0] Starting DoSomething | |
| [0] Starting long processing | |
| [6] Starting DoSomething | |
| [6] Starting long processing | |
| [7] Starting DoSomething | |
| [7] Starting long processing | |
| [8] Starting DoSomething | |
| [8] Starting long processing | |
| [1] Starting DoSomething | |
| [1] Starting long processing | |
| [3] Starting DoSomething | |
| [3] Starting long processing | |
| [2] Starting DoSomething | |
| [2] Starting long processing | |
| [4] Starting DoSomething | |
| [4] Starting long processing | |
| [5] Starting DoSomething | |
| [5] Starting long processing | |
| [9] Starting DoSomething | |
| [9] Starting long processing | |
| [9] Finished long processing | |
| [2] Finished long processing | |
| [8] Finished long processing | |
| [7] Finished long processing | |
| [0] Finished long processing | |
| [1] Finished long processing | |
| [3] Finished long processing | |
| [4] Finished long processing | |
| [6] Finished long processing | |
| [5] Finished long processing | |
| Processed 900 | |
| Processed 200 | |
| Processed 700 | |
| Processed 800 | |
| Processed 0 | |
| Processed 100 | |
| Processed 300 | |
| Processed 400 | |
| Processed 600 | |
| Final: [900 200 700 800 0 100 300 400 600] | |
| ------------------- | |
| Initial item count: 10 | |
| Final item count: 9 | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment