Last active
April 8, 2018 11:21
-
-
Save just1689/c7424243e9423cf12d9b67be5b2e3b56 to your computer and use it in GitHub Desktop.
Combining separate go routines results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From https://golangbot.com/channels/ | |
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