| 37 | } |
| 38 | |
| 39 | func main() { |
| 40 | // wg is used to manage concurrency. |
| 41 | var wg sync.WaitGroup |
| 42 | wg.Add(1) |
| 43 | |
| 44 | // Create a writer Goroutine that performs 10 different writes. |
| 45 | go func() { |
| 46 | for i := 0; i < 10; i++ { |
| 47 | time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) |
| 48 | writer(i) |
| 49 | } |
| 50 | wg.Done() |
| 51 | }() |
| 52 | |
| 53 | // Create eight reader Goroutines that runs forever. |
| 54 | for i := 0; i < 8; i++ { |
| 55 | go func(i int) { |
| 56 | for { |
| 57 | reader(i) |
| 58 | } |
| 59 | }(i) |
| 60 | } |
| 61 | |
| 62 | // Wait for the write Goroutine to finish. |
| 63 | wg.Wait() |
| 64 | fmt.Println("Program Complete") |
| 65 | } |
| 66 | |
| 67 | // writer adds a new string to the slice in random intervals. |
| 68 | func writer(i int) { |