今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
Channel non-blocking
- [Instructor] When we want to avoid blocking on sending on or receiving from a channel, we can use the default case for a select statement. Let's see it in action. We'll start by declaring a readChan to read from. The value doesn't matter so we'll use an empty struct as the type. Next, we'll use a select with a case to read from the channel. As is, this program will lead to a deadlock because there's nothing to read a value from the channel and the readChan operation blocks indefinitely. This is where the default case comes in. By adding a default case here, rephrase. By adding a default case, the select will simply move on. Rephrase, by adding a default case, the select will simply move on to that. If we can't read from the reach in and the program will simply continue on the same rephrase and the program will simply continue on. The same concepts apply for setting on a channel as well. Let's run a program. And here…