Skip to content

Instantly share code, notes, and snippets.

@kkamara
Last active November 27, 2023 01:16
Show Gist options
  • Save kkamara/7ce7812c224887a7b13a03f295bd88e7 to your computer and use it in GitHub Desktop.
Save kkamara/7ce7812c224887a7b13a03f295bd88e7 to your computer and use it in GitHub Desktop.
channel go routines
package main
import (
"fmt"
)
func calcSquares(number int, squareop chan int) {
sum := 0
for number != 0 {
digit := number % 10
sum += digit * digit
number /= 10
}
squareop <- sum
}
func calcCubes(number int, cubeop chan int) {
sum := 0
for number != 0 {
digit := number % 10
sum += digit * digit * digit
number /= 10
}
cubeop <- sum
}
func main() {
number := 589
sqrch := make(chan int)
cubech := make(chan int)
go calcSquares(number, sqrch)
go calcCubes(number, cubech)
squares, cubes := <-sqrch, <-cubech
fmt.Println("Final output", squares+cubes)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment