Concurrency examples
Concurrency examples 1. Go program that uses the sync package's WaitGroup type to wait for a group of goroutines to finish. The main function creates a WaitGroup value and calls its Add method with a value of 5 to specify that it is waiting for 5 goroutines to finish. Then it starts 5 goroutines by calling the Square function in a loop, passing the loop index and a pointer to the WaitGroup value as arguments. The Square function takes an integer no and a pointer to a WaitGroup value as arguments. It calculates the square of no and prints it along with the value of no. After that, it calls the Done method on the WaitGroup value to indicate that it has finished. The defer statement ensures that the Done method is called even if the function panics or returns early. Finally, the main function calls the Wait method on the WaitGroup value to block until all the goroutines have finished. This ensures that the program doesn't exit before all the goroutines have completed their work. ...