go routine example channels example 1

This commit is contained in:
Mert Gör ☭ 2024-01-22 04:12:30 +03:00
parent 9e917844ff
commit 7bf7c19c99
Signed by: hwpplayer1
GPG Key ID: 03E547D043AB6C8F
2 changed files with 30 additions and 0 deletions

17
channels/demo1.go Normal file
View File

@ -0,0 +1,17 @@
package channels
func EvenNumber(EvenNumberCn chan int) {
total := 0
for i := 0; i <= 10; i+=2 {
total = total + i
}
EvenNumberCn <- total
}
func OddNumber(OddNumberCn chan int) {
total := 0
for i := 1; i <= 10; i+=2 {
total = total + 1
}
OddNumberCn <- total
}

13
main.go
View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"golesson/arrays"
"golesson/channels"
"golesson/conditionals"
"golesson/examplerange"
"golesson/functions"
@ -62,4 +63,16 @@ func main() {
go goroutines.OddNumber()
time.Sleep(5 * time.Second)
fmt.Println("Main ended")
EvenNumberCn := make(chan int)
OddNumberCn := make(chan int)
go channels.EvenNumber(EvenNumberCn)
go channels.OddNumber(OddNumberCn)
EvenNumberTotal, OddNumberTotal := <- EvenNumberCn, <- OddNumberCn
multiply := EvenNumberTotal * OddNumberTotal
fmt.Println("Multiply Result : ", multiply)
}