Channels in Go
This is a study note for YouTube tutorial: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=20036s Channel basics You can only create channels using make function. ch := make ( chan int ) // chan int means create a integer channel Below is an example how to use channel to transfer data between goroutines. var wg = sync.WaitGroup{} func main () { ch := make ( chan int ) wg. Add ( 2 ) go func () { i := <-ch fmt. Println (i) wg. Done () }() go func () { ch <- 42 wg. Done () }() wg. Wait () } The result is: 42 Restricting data flow Be noted that channel operation will block the goroutine when executing. If you have unequal receiver and sender, then the additional goroutines will be blocked and give runtime errors. func main () { ch := make ( chan int ) wg. Add ( 3 ) go func () { i := <-ch fmt. Println (i) wg. Done () }() go func () { ...