From d46ca52504f8cf33b79a4777bfcdecb8ca061850 Mon Sep 17 00:00:00 2001 From: Selin Kurt Date: Mon, 21 Oct 2019 22:14:19 +0300 Subject: [PATCH] added channel examples with function --- examples/channels/channels-with-functions.go | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 examples/channels/channels-with-functions.go diff --git a/examples/channels/channels-with-functions.go b/examples/channels/channels-with-functions.go new file mode 100644 index 0000000..749b677 --- /dev/null +++ b/examples/channels/channels-with-functions.go @@ -0,0 +1,35 @@ +//In this example, we give a boolean value into a channel by calculating an another channel +//If the integer channel(the value is random) is divided into two, the value of boolean channel will be false +//otherwise it will be true +package main + +import ( + "fmt" + "math/rand" +) + +func channel_examples(number chan int, state chan bool) { + + number_value := <-number + state_value := <-state + + if number_value%2 == 0 { //if the number is divided into two + state_value = false + } + + fmt.Println(number_value, state_value) + +} + +func main() { + + number := make(chan int) + state := make(chan bool) + + go func() { + number <- rand.Intn(10) + state <- true + }() + channel_examples(number, state) + +}