Created
June 12, 2024 06:08
-
-
Save prdk0/c82ba84273fb8f88a5ba8c7f4cce87c9 to your computer and use it in GitHub Desktop.
add and even n natural sum using go channels
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 | |
import ( | |
"fmt" | |
"sync" | |
) | |
var wg sync.WaitGroup | |
func main() { | |
evenChan := make(chan int) | |
oddChan := make(chan int) | |
wg.Add(2) | |
go evenProcess(evenChan) | |
go oddProcess(oddChan) | |
fmt.Println(<-evenChan + <-oddChan) | |
wg.Wait() | |
} | |
func evenProcess(echan chan int) { | |
result := 0 | |
for i := 0; i <= 10; i += 2 { | |
result += i | |
} | |
echan <- result | |
wg.Done() | |
} | |
func oddProcess(ochan chan int) { | |
result := 0 | |
for i := 1; i <= 10; i += 2 { | |
result += i | |
} | |
ochan <- result | |
wg.Done() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment