Skip to content

Instantly share code, notes, and snippets.

@prdk0
Created June 12, 2024 06:08
Show Gist options
  • Save prdk0/c82ba84273fb8f88a5ba8c7f4cce87c9 to your computer and use it in GitHub Desktop.
Save prdk0/c82ba84273fb8f88a5ba8c7f4cce87c9 to your computer and use it in GitHub Desktop.
add and even n natural sum using go channels
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