Created
November 6, 2022 19:42
-
-
Save ekusiadadus/90b991a6d4f948b702321b9a61e12b79 to your computer and use it in GitHub Desktop.
channel2
channel2
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
package main | |
func goroutine1(s []int, c chan int) { | |
sum := 0 | |
for _, v := range s { | |
sum += v | |
} | |
c <- sum | |
} | |
func goroutine2(s []int, c chan int) { | |
sum := 0 | |
for _, v := range s { | |
sum -= v | |
} | |
c <- sum | |
} | |
func main() { | |
s := []int{1, 2, 3, 4, 5} | |
c := make(chan int) | |
go goroutine1(s, c) | |
go goroutine2(s, c) | |
x := <-c | |
y := <-c | |
println(x) | |
println(y) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment