Solution Review: Sum of Squares

Let's go over the solution of the Sum of Squares problem using `select` statements.

Press + to interact
package main
import "fmt"
func SumOfSquares(c, quit chan int) {
y := 1
for {
select {
case c <- (y*y):
y++
case <-quit:
return
}
}
}
func main() {
mychannel := make(chan int)
quitchannel:= make(chan int)
sum:= 0
go 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.