Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's difference between select{} and for{} in golang?

Tags:

go

I find a similar question: What does an empty select do?

for{} will cover 100% cpu usage.

select{} ask the channel case can read or write or not. But no case in the select. select{} will use 0% cpu usage? Or just like the for{}?

like image 375
g10guang Avatar asked Feb 13 '18 14:02

g10guang


1 Answers

for{} uses 100% CPU because it continuously executes the loop iteration.

select{} uses nearly 0% CPU because it causes the goroutine to block, which means the scheduler puts it to sleep, and it will never be woken.

like image 54
Adrian Avatar answered Oct 15 '22 19:10

Adrian