课程: Applied Concurrency in Go

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Channel basics

Channel basics

- [Instructor] So far, we've learned how to make use of goroutines and synchronize them using the sync package. Good stuff. While these logs are useful, channels are a concurrency synchronization mechanism that is unique to Go. Let's explore them in this section. Let's consider the following scenario. The DoWork function is invoked by main and it then calculates a value that is needed for the DoMoreWork function for recalculation. How might the information exchange take place if the two functions don't directly call each other? The usual way would be for the DoWork function to return the calculated value, save it in the scope of the main function after which main invokes the DoMoreWork function and passes it the correct value. The functions are communicating via the shared memory of the main function. If the functions are run in goroutines, they may end up interleaving or overwriting results. We use the wait groups…

内容