From the course: Go Essentials: Concurrency, Connectivity, and High-Performance Apps

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Select

Select

- The built in select, let us work with several channels at the same time, let's have a look. So here I'm creating two channels, channel one and channel two. I'm creating a function that is sending a value to channel one. And then I'm using select for case we got the value from channel one and case we got the value from channel two. What select is going to do is once one of these channels becomes available, and this is true both for receiving and sending, it is going to enter the specific case. So if you're going to run this code we're going to see that we got 42 from channel one. One thing that select is used for is timeouts. So we create a channel of float64 and then we create a function that is going to sleep a hundred millisecond and send the value of pie to the channel. And now we are going to say, if we got the value from the channel, meaning the goroutine finished in time, we are going to print out the value.…

Contents