
The Secret Life of Go: The Select Statement
How to stop fast data from waiting on slow channels Part 25: The Multiplexer, The Timeout, and The Non-Blocking Read Ethan was watching his terminal output drip line by line. It was agonizingly slow. "I don't understand," he said, rubbing his eyes. "I have two goroutines sending data. One is a local cache that returns in one millisecond. The other is a network call that takes five seconds. But the fast data is waiting for the slow data." Eleanor walked over and looked at his code. The Problem Code: func process ( cacheChan <- chan string , netChan <- chan string ) { // Read from the network (takes 5 seconds) netData := <- netChan fmt . Println ( "Received:" , netData ) // Read from the cache (takes 1 millisecond) cacheData := <- cacheChan fmt . Println ( "Received:" , cacheData ) } "You have created a traffic jam," Eleanor observed. "Channel receives are blocking. Because you asked for netChan first, your function halts right there. It doesn't matter that cacheChan has been ready for 4
Continue reading on Dev.to
Opens in a new tab

