Created
September 8, 2022 23:19
-
-
Save ejherran/86c3f4678c8f533607572b75d2932c98 to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
"math/rand" | |
"time" | |
) | |
type Unit struct { | |
value int32 | |
oddChan chan int32 | |
eveChan chan int32 | |
} | |
var serverChan = make(chan Unit, 10) | |
var generator = rand.New(rand.NewSource(time.Now().UnixNano())) | |
func Server() { | |
for { | |
unit := <-serverChan | |
fmt.Print("Received: ", unit.value) | |
if unit.value%2 == 0 { | |
unit.eveChan <- unit.value | |
} else { | |
unit.oddChan <- unit.value | |
} | |
fmt.Print("\tDispatched!.\n") | |
time.Sleep(3 * time.Second) | |
} | |
} | |
func main() { | |
oddChan := make(chan int32) | |
evenChan := make(chan int32) | |
var charges [10]int32 | |
for i := 0; i < 10; i++ { | |
charges[i] = generator.Int31n(100) | |
} | |
go Server() | |
for _, s := range charges { | |
serverChan <- Unit{s, oddChan, evenChan} | |
select { | |
case oddVal := <-oddChan: | |
fmt.Println("\t ", oddVal, "\tIs Odd.") | |
case evenVal := <-evenChan: | |
fmt.Println("\t ", evenVal, "\tIs Even.") | |
} | |
fmt.Print("------------------------\n\n") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment