From 569b255bfa90cfb79fe6c249c583c8ec1f9ce0eb Mon Sep 17 00:00:00 2001 From: Raul Date: Fri, 8 Mar 2024 10:26:58 +0000 Subject: [PATCH] Added delayed-channel exercise --- delayed-channel/go.mod | 3 +++ delayed-channel/main.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 delayed-channel/go.mod create mode 100644 delayed-channel/main.go diff --git a/delayed-channel/go.mod b/delayed-channel/go.mod new file mode 100644 index 0000000..147535f --- /dev/null +++ b/delayed-channel/go.mod @@ -0,0 +1,3 @@ +module delayed-channel + +go 1.22.1 diff --git a/delayed-channel/main.go b/delayed-channel/main.go new file mode 100644 index 0000000..bc8ba05 --- /dev/null +++ b/delayed-channel/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "time" +) + +func delayerSum(c chan int) { + val := 0 + for i := 0; i < 10; i++ { + val = val + 1 + } + time.Sleep(time.Second * 5) + c <- val +} + +func main() { + fmt.Println("Starting the delayed function, please wait") + loading := "Loading." + + for i := 0; i < 2; i++ { + fmt.Printf("\r%v", loading) + loading = loading + "." + time.Sleep(time.Second) + } + + delSum := make(chan int) + go delayerSum(delSum) + + finalValue := <-delSum + fmt.Println(finalValue) +}