whats the difference between for loop with select and only select?

Issue

Could not fully understand it from the docs or google:

What are the differences between the two and in
Which case would you use each one?

    for{
       select{
       case s := <-something:
           fmt.Println(s)
       case done := <-true:
           return 
       }
    }

and

       select{
       case s := <-something:
           fmt.Println(s)
       case done := <-true:
           return 
       }

Thanks

Solution

Code with loop will keep printing data from channel something until it receives anything on channel done.

Select-only code will either print data from channel something or will quit when it receives anything on channel done. Only one case will be executed. Keep in mind there is no fallthrough in Go select and switch statements by default.

Answered By – Grzegorz ┼╗ur

Answer Checked By – David Goodson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.