Solution Review: Sum of Squares
Let's go over the solution of the Sum of Squares problem using `select` statements.
package mainimport "fmt"func SumOfSquares(c, quit chan int) {y := 1for {select {case c <- (y*y):y++case <-quit:return}}}func main() {mychannel := make(chan int)quitchannel:= make(chan int)sum:= 0go func() {for i := 1; i <= 5; i++ {sum += <-mychannel}fmt.Println(sum)quitchannel <- 0}()SumOfSquares(mychannel, quitchannel)}
Get hands-on with 1400+ tech skills courses.